Skip to content

Commit f99ff62

Browse files
committed
Feature: Reintroduce photo evaluation function
1 parent 79d6b88 commit f99ff62

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

auto_comment_plus.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def all_evaluate(opts=None):
171171
num = ev.xpath('b/text()')[0]
172172
opts['logger'].debug('num: %s', num)
173173
except IndexError:
174-
opts['logger'].warning(
174+
opts['logger'].info(
175175
'Can\'t find num content in XPath, fallback to 0')
176176
num = 0
177177
N[na] = int(num)
@@ -245,18 +245,55 @@ def ordinary(N, opts=None):
245245
opts['logger'].debug('Loop: %d / %d', idx + 1, loop_times1)
246246
pid = pid.replace('//item.jd.com/', '').replace('.html', '')
247247
opts['logger'].debug('pid: %s', pid)
248-
opts['logger'].info(f"\t{i}.开始评价订单\t{oname}[{oid}]")
248+
opts['logger'].info(f"\t{i}.开始评价订单\t{oname}[{oid}]并晒图")
249249
url2 = "https://club.jd.com/myJdcomments/saveProductComment.action"
250250
opts['logger'].debug('URL: %s', url2)
251251
xing, Str = generation(oname, opts=opts)
252252
opts['logger'].info(f'\t\t评价内容,星级{xing}:' + Str)
253+
254+
#获取图片
255+
opts['logger'].info(f'\t\t开始获取图片')
256+
url1 = (f'https://club.jd.com/discussion/getProductPageImageCommentList'
257+
f'.action?productId={pid}')
258+
opts['logger'].debug('Fetching images using the default URL')
259+
opts['logger'].debug('URL: %s', url1)
260+
req1 = requests.get(url1, headers=headers)
261+
opts['logger'].debug(
262+
'Successfully accepted the response with status code %d',
263+
req1.status_code)
264+
if not req.ok:
265+
opts['logger'].warning(
266+
'Status code of the response is %d, not 200', req1.status_code)
267+
imgdata = req1.json()
268+
opts['logger'].debug('Image data: %s', imgdata)
269+
if imgdata["imgComments"]["imgCommentCount"] == 0:
270+
opts['logger'].debug('Count of fetched image comments is 0')
271+
opts['logger'].debug('Fetching images using another URL')
272+
url1 = ('https://club.jd.com/discussion/getProductPageImage'
273+
'CommentList.action?productId=1190881')
274+
opts['logger'].debug('URL: %s', url1)
275+
req1 = requests.get(url1, headers=headers)
276+
opts['logger'].debug(
277+
'Successfully accepted the response with status code %d',
278+
req1.status_code)
279+
if not req.ok:
280+
opts['logger'].warning(
281+
'Status code of the response is %d, not 200',
282+
req1.status_code)
283+
imgdata = req1.json()
284+
opts['logger'].debug('Image data: %s', imgdata)
285+
imgurl = imgdata["imgComments"]["imgList"][0]["imageUrl"]
286+
opts['logger'].debug('Image URL: %s', imgurl)
287+
opts['logger'].info(f'\t\t图片url={imgurl}')
288+
253289
data2 = {
254290
'orderId': oid,
255291
'productId': pid, # 商品id
256292
'score': str(xing), # 商品几星
257293
'content': bytes(Str, encoding="gbk"), # 评价内容
258294
'saveStatus': '1',
259-
'anonymousFlag': '1'
295+
'anonymousFlag': '1', # 是否匿名
296+
'imgs': imgurl, # 图片url
260297
}
261298
opts['logger'].debug('Data: %s', data2)
262299
if not opts.get('dry_run'):
@@ -265,6 +302,7 @@ def ordinary(N, opts=None):
265302
else:
266303
opts['logger'].debug(
267304
'Skipped sending comment request in dry run')
305+
opts['logger'].info(f"\t{i}.评价订单\t{oname}[{oid}]并晒图成功")
268306
opts['logger'].debug('Sleep time (s): %.1f', ORDINARY_SLEEP_SEC)
269307
time.sleep(ORDINARY_SLEEP_SEC)
270308
idx += 1
@@ -499,7 +537,11 @@ def Service_rating(N, opts=None):
499537
opts['logger'].debug('Commenting on items')
500538
for i, Order in enumerate(Order_data):
501539
oname = Order.xpath('td[1]/div[1]/div[2]/div/a/text()')[0]
502-
oid = Order.xpath('td[4]/div/a[1]/@oid')[0]
540+
try:
541+
oid = Order.xpath('td[4]/div/a[1]/@oid')[0]
542+
except IndexError:
543+
opts['logger'].warning('Failed to fetch oid')
544+
continue
503545
opts['logger'].info(f'\t开始第{i+1}{oname}')
504546
opts['logger'].debug('oid: %s', oid)
505547
url1 = (f'https://club.jd.com/myJdcomments/insertRestSurvey.action'
@@ -564,7 +606,7 @@ def main(opts=None):
564606
opts['logger'].debug('N value after executing No(): %s', N)
565607
'''
566608
if N['待追评'] != 0:
567-
opts['logger'].info("3.开始批量追评")
609+
opts['logger'].info("3.开始批量追评,注意:追评不会自动上传图片")
568610
N = review(N, opts)
569611
opts['logger'].debug('N value after executing review(): %s', N)
570612
N = No(opts)

jdspider.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ def getData(self, maxPage, score,): # maxPage是爬取评论的最大页数,
8787

8888
comments = []
8989
scores = []
90-
if len(self.productsId) < 10: # limit the sum of products
90+
default_logger.info('爬取商品数量最多为8个,请耐心等待,也可以自行修改jdspider文件')
91+
if len(self.productsId) < 8: # limit the sum of products
9192
sum = len(self.productsId)
9293
else:
93-
sum = 10
94+
sum = 8
9495
for j in range(sum):
9596
id = self.productsId[j]
9697
header = self.getHeaders(id)
9798
for i in range(1, maxPage):
9899
param, url = self.getParamUrl(id, i, score)
99-
default_logger.info("正在爬取评论信息>>>>>>>>>第:%d 个,第 %d 页" % (j, i))
100+
default_logger.info(f"正在爬取当前商品的评论信息>>>>>>>>>第:%d 个,第 %d 页" % (j, i))
100101
try:
101102
response = requests.get(url, headers=header, params=param)
102103
except Exception as e:
@@ -115,10 +116,8 @@ def getData(self, maxPage, score,): # maxPage是爬取评论的最大页数,
115116
default_logger.warning(e)
116117
continue
117118
if len((res_json['comments'])) == 0:
118-
default_logger.warning("页面次数已到:%d,超出范围" % (i))
119+
default_logger.warning("页面次数已到:%d,超出范围(或未爬取到评论)" % (i))
119120
break
120-
default_logger.info("正在爬取%s %s 第 %d" %
121-
(self.categlory, self.comtype[score], i))
122121
for cdit in res_json['comments']:
123122
comment = cdit['content'].replace(
124123
"\n", ' ').replace('\r', ' ')

0 commit comments

Comments
 (0)