This commit is contained in:
dylan 2022-10-02 23:22:38 +08:00
parent ae95af0e81
commit b659c764b8
1 changed files with 79 additions and 66 deletions

View File

@ -238,43 +238,40 @@ def sunbw(N, opts=None):
'//*[@id="main"]/div[2]/div[2]/table') '//*[@id="main"]/div[2]/div[2]/table')
opts['logger'].debug('Count of fetched order data: %d', len(elems)) opts['logger'].debug('Count of fetched order data: %d', len(elems))
Order_data.extend(elems) Order_data.extend(elems)
opts['logger'].info(f"当前共有{N['待评价订单']}个需要评价晒单。")
opts['logger'].info(f"当前共有{N['待评价订单']}个评价。")
opts['logger'].debug('Commenting on items') opts['logger'].debug('Commenting on items')
for i, Order in enumerate(Order_data): for i, Order in enumerate(Order_data):
if i +1 > 10: if i + 1 > 10:
opts['logger'].info(f'\t已评价10个订单跳出') opts['logger'].info(f'\t已评价10个订单跳出')
break break
oname = Order.xpath( try:
'tr[@class="tr-bd"]/td[1]/div[1]/div[2]/div/a/text()')[0] oid = Order.xpath('tr[@class="tr-th"]/td/span[3]/a/text()')[0]
pid = Order.xpath( opts['logger'].debug('oid: %s', oid)
'tr[@class="tr-bd"]/td[1]/div[1]/div[2]/div/a/@href')[0].replace('//item.jd.com/', '').replace('.html', '') oname_data = Order.xpath(
oid = Order.xpath('tr[@class="tr-th"]/td/span[3]/a/text()')[0] 'tr[@class="tr-bd"]/td[1]/div[1]/div[2]/div/a/text()')
#print (oname,oid,pid) opts['logger'].debug('oname_data: %s', oname_data)
#sys.exit() pid_data = Order.xpath(
opts['logger'].info(f'\t开始第{i+1}个订单: {oid}') 'tr[@class="tr-bd"]/td[1]/div[1]/div[2]/div/a/@href')
opts['logger'].debug('pid: %s', pid) opts['logger'].debug('pid_data: %s', pid_data)
opts['logger'].debug('oid: %s', oid) except IndexError:
xing, Str = generation(oname, opts=opts) opts['logger'].warning(f"{i + 1}个订单未查找到商品,跳过。")
opts['logger'].info(f'\t评价信息:{xing}' + Str) continue
# 获取图片 loop_times1 = min(len(oname_data), len(pid_data))
url1 = (f'https://club.jd.com/discussion/getProductPageImageCommentList' opts['logger'].debug('Commenting on orders')
f'.action?productId={pid}') opts['logger'].debug('Total loop times: %d', loop_times1)
opts['logger'].debug('Fetching images using the default URL') idx = 0
opts['logger'].debug('URL: %s', url1) for oname, pid in zip(oname_data, pid_data):
req1 = requests.get(url1, headers=headers) pid = pid.replace('//item.jd.com/', '').replace('.html', '')
opts['logger'].debug( opts['logger'].info(f'\t开始第{i+1}个订单: {oid}')
'Successfully accepted the response with status code %d', opts['logger'].debug('pid: %s', pid)
req1.status_code) opts['logger'].debug('oid: %s', oid)
if not req.ok: xing, Str = generation(oname, opts=opts)
opts['logger'].warning( opts['logger'].info(f'\t评价信息:{xing}' + Str)
'Status code of the response is %d, not 200', req1.status_code) # 获取图片
imgdata = req1.json() url1 = (f'https://club.jd.com/discussion/getProductPageImageCommentList'
opts['logger'].debug('Image data: %s', imgdata) f'.action?productId={pid}')
if imgdata["imgComments"]["imgCommentCount"] == 0: opts['logger'].debug('Fetching images using the default URL')
opts['logger'].debug('Count of fetched image comments is 0')
opts['logger'].debug('Fetching images using another URL')
url1 = ('https://club.jd.com/discussion/getProductPageImage'
'CommentList.action?productId=1190881')
opts['logger'].debug('URL: %s', url1) opts['logger'].debug('URL: %s', url1)
req1 = requests.get(url1, headers=headers) req1 = requests.get(url1, headers=headers)
opts['logger'].debug( opts['logger'].debug(
@ -282,41 +279,57 @@ def sunbw(N, opts=None):
req1.status_code) req1.status_code)
if not req.ok: if not req.ok:
opts['logger'].warning( opts['logger'].warning(
'Status code of the response is %d, not 200', 'Status code of the response is %d, not 200', req1.status_code)
req1.status_code)
imgdata = req1.json() imgdata = req1.json()
opts['logger'].debug('Image data: %s', imgdata) opts['logger'].debug('Image data: %s', imgdata)
imgurl = imgdata["imgComments"]["imgList"][0]["imageUrl"] if imgdata["imgComments"]["imgCommentCount"] == 0:
opts['logger'].debug('Image URL: %s', imgurl) opts['logger'].debug('Count of fetched image comments is 0')
opts['logger'].debug('Fetching images using another URL')
url1 = ('https://club.jd.com/discussion/getProductPageImage'
'CommentList.action?productId=1190881')
opts['logger'].debug('URL: %s', url1)
req1 = requests.get(url1, headers=headers)
opts['logger'].debug(
'Successfully accepted the response with status code %d',
req1.status_code)
if not req.ok:
opts['logger'].warning(
'Status code of the response is %d, not 200',
req1.status_code)
imgdata = req1.json()
opts['logger'].debug('Image data: %s', imgdata)
imgurl = imgdata["imgComments"]["imgList"][0]["imageUrl"]
opts['logger'].debug('Image URL: %s', imgurl)
opts['logger'].info(f'\t图片:{imgurl}') opts['logger'].info(f'\t图片:{imgurl}')
# 提交晒单 # 提交晒单
opts['logger'].debug('Preparing for commenting') opts['logger'].debug('Preparing for commenting')
url2 = "https://club.jd.com/myJdcomments/saveProductComment.action" url2 = "https://club.jd.com/myJdcomments/saveProductComment.action"
opts['logger'].debug('URL: %s', url2) opts['logger'].debug('URL: %s', url2)
headers['Referer'] = ('https://club.jd.com/myJdcomments/orderVoucher.action') headers['Referer'] = ('https://club.jd.com/myJdcomments/orderVoucher.action')
headers['Origin'] = 'https://club.jd.com' headers['Origin'] = 'https://club.jd.com'
headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['Content-Type'] = 'application/x-www-form-urlencoded'
opts['logger'].debug('New header for this request: %s', headers) opts['logger'].debug('New header for this request: %s', headers)
data = { data = {
'orderId': oid, 'orderId': oid,
'productId': pid, 'productId': pid,
'score': str(xing), # 商品几星 'score': str(xing), # 商品几星
'content': bytes(Str, encoding="gbk"), # 评价内容 'content': bytes(Str, encoding="gbk"), # 评价内容
'imgs': imgurl, 'imgs': imgurl,
'saveStatus': 2, 'saveStatus': 2,
'anonymousFlag': 1 'anonymousFlag': 1
} }
opts['logger'].debug('Data: %s', data) opts['logger'].debug('Data: %s', data)
if not opts.get('dry_run'): if not opts.get('dry_run'):
opts['logger'].debug('Sending comment request') opts['logger'].debug('Sending comment request')
req_url2 = requests.post(url2, data=data, headers=headers) pj2 = requests.post(url2, headers=headers, data=data)
else: else:
opts['logger'].debug('Skipped sending comment request in dry run') opts['logger'].debug(
opts['logger'].info('完成') 'Skipped sending comment request in dry run')
opts['logger'].debug('Sleep time (s): %.1f', SUNBW_SLEEP_SEC) opts['logger'].debug('Sleep time (s): %.1f', ORDINARY_SLEEP_SEC)
time.sleep(SUNBW_SLEEP_SEC) time.sleep(ORDINARY_SLEEP_SEC)
N['待评价订单'] -= 1 idx += 1
N['待评价订单'] -= 1
return N return N