作者:XXX白米饭
来源:Python技术
在JD.COM完成的订单评估会返还部分精豆作为货币。边肖也是一个懒惰的人。她不喜欢拍照,也不喜欢评价任何产品。她已经半年没有评价任务的产品了。一个一个评估太麻烦了,所以我写了一个python脚本自动完成。
第一步是在脚本中登录JD.COM。这里使用的是从浏览器中登录的JD.COM帐户获取cookie,并将其复制到标头中。
# -*-编码:utf-8-*- 来自bs4的导入请求 import beautiful group 导入时间 导入JSON headers = { & # 39;饼干& # 39;: '自己的饼干& # 39;, & # 39;用户代理& # 39;: 'Mozilla/5.0(Windows NT 10.0;Win64x64) AppleWebKit/537.36 (KHTML,像壁虎一样)Chrome/95 . 0 . 4638 . 69 Safari/537.36 & # 39;, } 获取所有评价JD.COM的评价页面上有四种评价,包括待评价的订单、待晒的订单、后续评价和服务评价。每个评价后面都会有一个数字,表示有多少评价还没有写出来。
用BeautifulSoup抓取这些内容来确定是否有评价要填。最后,将捕获的数据放入字典。以后用这个号码作为寻呼依据。
def all _ appraisal(): appraisal = { } URL = & # 34;https://club.jd.com/myJdcomments/myJdcomment.action?排序= 0 & # 34; req = requests.get(url,headers = headers) soup = beautiful soup(req . text,& # 34;html.parser & # 34) URL = soup . find(& # 39;ul & # 39,class _ = & # 39tab-触发器& # 39;); 为李在URL . find _ all(& # 39;李& # 39;): contents = Li . a . text b = Li . b if b!=无: 考核[内容] = b.text 返回考核 样本结果:
{'待评估订单& # 39;: '17', '要烘干的清单& # 39;: '1', '待评估& # 39;: '68', '服务评估& # 39;: '27'} 为了被评估在控制面板的网络中,找到提交评估的url地址(https://club . JD . com/myjd comments/save product comment . action)。可以看到需要的post参数有:订单号、产品号、提交内容和星数。这里先不发图评了,后面的印品评测我们一起做。
Get class _ = & # 39TD-无效订单-TB & # 39;表获取每一行的订单信息,然后将其解析为订单号、产品号和产品名。在提交内容的时候,在网上搜索一个通用的产品评测模板,星数是五颗。
def be _ evaluated(): appraisal = all _ appraisal() for I in range((appraisal[& # 39;待评估订单& # 39;]//20)+1): URL = & # 39;https://club.jd.com/myJdcomments/myJdcomment.action?排序= 0 & page = { } & # 39。format(I+1) req = requests . get(URL,headers = headers) soup = beautiful soup(req . text,& # 34;html.parser & # 34) table = soup . find(& # 39;表& # 39;,class _ = & # 39TD-无效订单-TB & # 39;); tbodys = table . find _ all(& # 39;tbody & # 39) for order in tbodys: oid = order . find(& # 39;span & # 39,class _ = & # 34数量& # 34;). a . text product = order . find(& # 39;div & # 39,class _ = & # 39p-name & # 39;).a pname = product . text PID = product[& # 39;href & # 39].替换(& # 39;//item . JD . com/& # 39;, '').替换(& # 39;。html & # 39, '') content = pname+& # 39;东西质量很好,完全符合卖家的描述,很满意,真的很喜欢,完全超出预期,发货非常快,包装非常细致严密,物流公司服务态度好,发货非常快,购物非常满意& # 39; save product comment _ URL = & # 34;https://club . JD . com/myjd comments/save product comment . action & # 34; save product comment _ data = { & # 39;订单编号& # 39;:oid, & # 39;productId & # 39:pid, & # 39;得分& # 39;: '5', & # 39;内容& # 39;:字节(内容,编码= & # 34;gbk & # 34), & # 39;保存状态& # 39;: '1', & # 39;anonymousFlag & # 39: '1' } save = requests . post(save product comment _ URL,headers = headers,data = save product comment _ data) time . sleep(5) 要烘干的列表[//]COMT-plists & # 34;Div,每个订单都是一个class = & # 34COMT-plist & # 34;Div。用bs4很容易搞定。
作为一个python脚本,您怎么可能需要制作自己的产品地图呢?这里(https://club.jd.com/discussion/getproductpage图片内容列表. action)下载别人的产品图片粘贴到自己的产品评价里,返回一个json字符串。解析后获取第一个imageUrl。
def be_shown_img():
url = 'https://club.jd.com/myJdcomments/myJdcomment.action?sort=1'
req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text, "html.parser")
pro_info = soup.find_all('div', class_ = 'pro-info');
for plist in pro_info:
oid = plist['oid']
pid = plist['pid']
img_url = 'https://club.jd.com/discussion/getProductPageImageCommentList.action?productId={}'.format(pid)
img_req = requests.get(img_url, headers=headers)
text = img_req.text
print(img_url)
result = json.loads(text)
imgurl = result["imgComments"]["imgList"][0]["imageUrl"]
saveUrl = 'https://club.jd.com/myJdcomments/saveShowOrder.action'
img_data = {
'orderId': oid,
'productId': pid,
'imgs': imgurl,
'saveStatus': 3
}
print(img_data)
headers['Referer'] = 'https://club.jd.com/myJdcomments/myJdcomment.action?sort=1'
headers['Origin'] = 'https://club.jd.com'
headers['Content-Type'] = 'application/x-www-form-urlencoded'
requests.post(saveUrl, data=img_data, headers=headers)
time.sleep(5)
待追评 追评和评价差不多,不需要上传图片,post 参数只要取得订单号和商品号就可以了。def be _ showed _ img():
URL = & # 39;https://club.jd.com/myJdcomments/myJdcomment.action?排序= 1 & # 39;
req = requests.get(url,headers = headers)
soup = beautiful soup(req . text,& # 34;html.parser & # 34)
pro _ info = soup . find _ all(& # 39;div & # 39,class _ = & # 39pro-info & # 39;);
for plist in pro _ info:
oid = plist[& # 39;oid & # 39]
PID = plist[& # 39;pid & # 39]
img _ URL = & # 39;https://club . JD . com/discussion/getproductpageimagecommentlist . action?productId = { } & # 39。format(PID)
img _ req = requests . get(img _ URL,headers = headers)
text = img _ req . text
print(img _ URL)
result = JSON . loads(text)
imgurl = result[& # 34;imgComments & # 34]["imgList & # 34][0]["imageUrl & # 34]
save URL = & # 39;https://club.jd.com/myJdcomments/saveShowOrder.action'
img _ data = {
& # 39;订单编号& # 39;:oid,
& # 39;productId & # 39:pid,
& # 39;imgs & # 39:imgurl,
& # 39;保存状态& # 39;:3
}
print(img _ data)
headers[& # 39;Referer & # 39] = 'https://club.jd.com/myJdcomments/myJdcomment.action?排序= 1 & # 39;
标题[& # 39;起源& # 39;] = 'https://club . JD . com & # 39;
标题[& # 39;内容类型& # 39;] = 'application/x-www-form-urlencoded & # 39;
个请求。post (saveurl,data = img _ data,headers = headers)
time。睡眠(5)
好像没有专门的标签来获取订单号和产品号,只能在评论按钮的href里截取。
def review():
appraisal = all_appraisal()
saveUrl = 'https://club.jd.com/afterComments/saveAfterCommentAndShowOrder.action'
for i in range((appraisal['待评价订单'] // 20) + 1):
url = 'https://club.jd.com/myJdcomments/myJdcomment.action?sort=3&page={}'.format(i+1)
req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text, "html.parser")
operates = soup.find_all('div', class_='operate')
for o in operates:
href = o.a['href']
infos = href.replace('http://club.jd.com/afterComments/productPublish.action?sku=','').split('&orderId=');
pid = infos[0]
oid = infos[1]
data = {
'orderId': oid,
'productId': pid,
'content': bytes('宝贝和想象中差不多所以好评啦,对比了很多家才选择了这款,还是不错的,很NICE!真的', encoding='gbk'),
'imgs': '',
'anonymousFlag': 1,
'score': 5
}
requests.post(saveUrl, headers=headers, data=data)
time.sleep(5)
服务评价 服务评价的提交很简单,参数只要一个订单号就可以了,只需解析下图的 html。def review():
appraisal = all _ appraisal()
save URL = & # 39;https://club . JD . com/after comments/saveaftecontendshoworder . action & # 39;
对于范围内的I((评估[& # 39;待评估订单& # 39;]//20)+1):
URL = & # 39;https://club.jd.com/myJdcomments/myJdcomment.action? sort = 3 & page = { } & # 39;。format(I+1)
req = requests . get(URL,headers = headers)
soup = beautiful soup(req . text,& # 34;html.parser & # 34)
operations = soup . find _ all(& # 39;div & # 39,class _ = & # 39操作& # 39;)
for o in operates:
href = o . a[& # 39;href & # 39]
infos = href . replace(& # 39;http://club.jd.com/afterComments/productPublish.action? SKU = & # 39;,'').拆分(& # 39;& orderId = & # 39);
PID = infos[0]
oid = infos[1]
data = {
& # 39;订单编号& # 39;:oid,
& # 39;productId & # 39:pid,
& # 39;内容& # 39;:字节(& # 39;宝贝几乎和我想象的一样好,所以好评如潮。对比了很多家之后,我选择了这家。它仍然是好的,非常好!真的& # 39;,编码= & # 39;gbk & # 39),
& # 39;imgs & # 39: '',
& # 39;anonymousFlag & # 39:1,
& # 39;得分& # 39;:5
}
requests . post(save URL,headers = headers,data = data)
time . sleep(5)
其他参数可以写死。快递包裹五星评分1827A1,配送速度1828A1,配送服务价值1829A1。
def service _ rating(): appraisal = all _ appraisal() save URL = & # 39;https://club.jd.com/myJdcomments/insertRestSurvey.action? vote id = 145 & rule id = { } & # 39; 对于范围内的I((评估[& # 39;服务评估& # 39;]//20)+1): URL = & # 34;https://club.jd.com/myJdcomments/myJdcomment.action? sort = 4 & page = { } & # 34;。format(I+1) req = requests . get(URL,headers = headers) soup = beautiful soup(req . text,& # 34;html.parser & # 34) TRS = soup . find _ all(& # 39;tr & # 39,class _ = & # 39tr-th & # 39;); for tr in TRS: oid = tr . find(& # 39;span & # 39,class _ = & # 39数量& # 39;). a . text save URL = save URL . format(oid) data = { & # 39;oid & # 39:oid, & # 39;gid & # 39:69, & # 39;席德& # 39;:549656, & # 39;stid & # 39:0, & # 39;标签& # 39;: '', & # 39;罗1827 & # 39;: '1827A1 & # 39, & # 39;罗1828 & # 39;: '1828A1 & # 39, & # 39;罗1829 & # 39;: '1829A1 & # 39, } requests . post(save URL,headers=headers,data = data) print(& # 39;订单号: '+oid+& # 39;服务评估完成& # 39;) time.sleep(5) 总结 JD。COM的商品评测脚本比较容易,只用到了requests和bs4的第三方模块,加密解密什么都没有。非常适合刚学爬虫的小伙伴练手。