來(lái)源:Python 技術(shù)
在京東上完成訂單的評(píng)價(jià)都會(huì)返還一些京豆當(dāng)錢用。小編也是一個(gè)懶人,不喜歡拍照和評(píng)價(jià)任何商品,半年都沒(méi)有去評(píng)價(jià)任務(wù)的商品了。一個(gè)個(gè)評(píng)價(jià)太麻煩了,就寫了一個(gè) python 腳本自動(dòng)完成。
首先就是要在腳本上登錄京東,這里用的是把在瀏覽器登錄的京東賬號(hào)取到 cookie 后復(fù)制到 header 上。
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
import json
headers = {
'cookie': '自己 cookie',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
}
在京東的評(píng)價(jià)頁(yè)面一共有 4 種評(píng)價(jià),待評(píng)價(jià)訂單、待曬單待、追評(píng)和服務(wù)評(píng)價(jià),每個(gè)評(píng)價(jià)后面會(huì)跟上數(shù)字,表示還有多少個(gè)評(píng)價(jià)沒(méi)寫。
使用 BeautifulSoup 抓取這些內(nèi)容用于判斷是否有需要填寫的評(píng)價(jià)。最終把抓取的數(shù)據(jù)放到字典中。后面用這個(gè)數(shù)字做分頁(yè)基礎(chǔ)。
def all_appraisal():
appraisal = {}
url = "https://club.jd.com/myJdcomments/myJdcomment.action?sort=0"
req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text, "html.parser")
url = soup.find('ul', class_='tab-trigger');
for li in url.find_all('li'):
contents = li.a.text
b = li.b
if b != None:
appraisal[contents] = b.text
return appraisal
示例結(jié)果:
{'待評(píng)價(jià)訂單': '17', '待曬單': '1', '待追評(píng)': '68', '服務(wù)評(píng)價(jià)': '27'}
在控制面板的網(wǎng)絡(luò)中,找到提交評(píng)價(jià)的 url 地址(https://club.jd.com/myJdcomments/saveProductComment.action)。可以看到需要的 post 參數(shù)有:訂單號(hào),商品號(hào),提交內(nèi)容和星星數(shù)。這里先不發(fā)圖片評(píng)價(jià),在后面的曬圖評(píng)價(jià)中一起做。
通過(guò)獲取 class_ = 'td-void order-tb' 的 table 獲取到每行的訂單信息,從而解析到訂單號(hào)、商品號(hào)和商品名稱,提交內(nèi)容就在網(wǎng)上搜一套萬(wàn)能的商品評(píng)價(jià)模板,星星數(shù)都是 5 個(gè)。
def be_evaluated():
appraisal = all_appraisal()
for i in range((appraisal['待評(píng)價(jià)訂單'] // 20) + 1):
url = 'https://club.jd.com/myJdcomments/myJdcomment.action?sort=0&page={}'.format(i + 1)
req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text, "html.parser")
table = soup.find('table', class_ = 'td-void order-tb');
tbodys = table.find_all('tbody')
for order in tbodys:
oid = order.find('span', class_="number").a.text
product = order.find('div', class_='p-name').a
pname = product.text
pid=product['href'].replace('//item.jd.com/', '').replace('.html', '')
content = pname + ',東西質(zhì)量非常好,與賣家描述的完全一致,非常滿意,真的很喜歡,完全超出期望值,發(fā)貨速度非???,包裝非常仔細(xì)、嚴(yán)實(shí),物流公司服務(wù)態(tài)度很好,運(yùn)送速度很快,很滿意的一次購(gòu)物'
saveProductComment_url = "https://club.jd.com/myJdcomments/saveProductComment.action"
saveProductComment_data = {
'orderId': oid,
'productId': pid,
'score': '5',
'content': bytes(content, encoding="gbk"),
'saveStatus': '1',
'anonymousFlag': '1'
}
save = requests.post(saveProductComment_url, headers=headers, data=saveProductComment_data)
time.sleep(5)
待曬單頁(yè)面中的訂單信息在 class="comt-plists" 的 div 中,每一個(gè)訂單都是一個(gè)個(gè) class="comt-plist" 的 div。用 bs4 很容易就獲取到了。
作為一個(gè) python 腳本,怎么可能需要自己拍商品圖呢?這里(https://club.jd.com/discussion/getProductPageImageCommentList.action)下載別人的商品圖片貼到自己的商品評(píng)價(jià)中,返回的是一個(gè) json 串。解析后得到第一個(gè) 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)
追評(píng)和評(píng)價(jià)差不多,不需要上傳圖片,post 參數(shù)只要取得訂單號(hào)和商品號(hào)就可以了。
追評(píng)似乎沒(méi)有特別的標(biāo)簽可以獲取訂單號(hào)和商品號(hào),只能在追評(píng)按鈕的 href 中截取。
def review():
appraisal = all_appraisal()
saveUrl = 'https://club.jd.com/afterComments/saveAfterCommentAndShowOrder.action'
for i in range((appraisal['待評(píng)價(jià)訂單'] // 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('寶貝和想象中差不多所以好評(píng)啦,對(duì)比了很多家才選擇了這款,還是不錯(cuò)的,很NICE!真的', encoding='gbk'),
'imgs': '',
'anonymousFlag': 1,
'score': 5
}
requests.post(saveUrl, headers=headers, data=data)
time.sleep(5)
服務(wù)評(píng)價(jià)的提交很簡(jiǎn)單,參數(shù)只要一個(gè)訂單號(hào)就可以了,只需解析下圖的 html。
其他的參數(shù)都可以被寫死,快遞包裝的 5 顆星得分的數(shù)值:1827A1,送貨速度是:1828A1,配送員服務(wù)的數(shù)值是:1829A1。
def service_rating():
appraisal = all_appraisal()
saveUrl = 'https://club.jd.com/myJdcomments/insertRestSurvey.action?voteid=145&ruleid={}'
for i in range((appraisal['服務(wù)評(píng)價(jià)'] // 20) + 1):
url = "https://club.jd.com/myJdcomments/myJdcomment.action?sort=4&page={}".format(i + 1)
req = requests.get(url, headers=headers)
soup = BeautifulSoup(req.text, "html.parser")
trs = soup.find_all('tr', class_='tr-th');
for tr in trs:
oid = tr.find('span', class_='number').a.text
saveUrl = saveUrl.format(oid)
data = {
'oid': oid,
'gid': 69,
'sid': 549656,
'stid': 0,
'tags': '',
'ro1827': '1827A1',
'ro1828': '1828A1',
'ro1829': '1829A1',
}
requests.post(saveUrl, headers=headers, data=data)
print('訂單號(hào):' + oid + '服務(wù)評(píng)價(jià)完成')
time.sleep(5)
京東的商品評(píng)價(jià)腳本比較容易,只用到了 requests 和 bs4 第三方模塊,也沒(méi)有什么加密解密的東西。非常適合剛學(xué)爬蟲(chóng)的小伙伴用來(lái)練手。
]]>