作者:某某白米飯
來(lái)源:Python 技術(shù)
在京東上完成訂單的評(píng)價(jià)都會(huì)返還一些京豆當(dāng)錢用。小編也是一個(gè)懶人,不喜歡拍照和評(píng)價(jià)任何商品,半年都沒有去評(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à)沒寫。
使用 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)似乎沒有特別的標(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 第三方模塊,也沒有什么加密解密的東西。非常適合剛學(xué)爬蟲的小伙伴用來(lái)練手。
? ? ? ? ? ? ? ? ? ? ? ? ? ?
加我微信: sumwb886 備注:?外包
免費(fèi)領(lǐng)推廣引流方案+100種賣貨方法
抖音句子上熱門,熱門抖音語(yǔ)錄?
人生中總會(huì)有一些意想不到的溫暖和源源不斷的希望。 山有木兮風(fēng)吹過(guò),你的心思我都明了。今夜星辰閃閃如你。 你建起…
抖音尺寸,短視頻尺寸規(guī)范?
抱歉,我無(wú)法滿足你的要求。 歡迎大家關(guān)注短視頻顧小雨頭條號(hào),想了解行業(yè)問(wèn)題,可以私信顧小雨,還能免費(fèi)獲取最新行…
國(guó)外抖音tiktok怎么用,國(guó)外tiktok怎么下載?
由于地理限制和網(wǎng)絡(luò)問(wèn)題,國(guó)內(nèi)用戶可能無(wú)法暢享抖音國(guó)際版TikTok。但是,通過(guò)使用TikTok加速器,您可以解…
抖音發(fā)不出去視頻怎么回事,視頻無(wú)法上傳抖音?
晚上好,我的朋友們!今天我遇到了一個(gè)問(wèn)題,就是為什么我做的視頻發(fā)不出去呢?通常我都會(huì)在剪輯和添加特效的過(guò)程中順…
抖音火山版正版下載安裝,抖音火山版下載?
根據(jù)中關(guān)村在線的消息,今天火山小視頻宣布與抖音進(jìn)行品牌整合。整合后,火山小視頻更名為抖音火山版,并推出全新的圖…
抖音粉,熱門抖音歌曲?
在抖音上,要開通櫥窗或者享受一些特權(quán),需要有一定數(shù)量的粉絲。然而,積累1000個(gè)或更多的原始粉絲并非易事。一些…
抖音怎么制作視頻教程,視頻制作技巧教程?
把一張照片制作成動(dòng)態(tài)視頻會(huì)讓它更生動(dòng)。 學(xué)習(xí)如何在照片基礎(chǔ)上制作出漂亮的視頻非常簡(jiǎn)單易學(xué)。您不需要拍攝視頻,也…
抖音店鋪保證金,抖音經(jīng)營(yíng)保證金?
抖音電商在抖音開店各個(gè)類目需要繳納的保證金有很多種,具體金額需要根據(jù)不同的類目而定。一般來(lái)說(shuō),保證金的金額會(huì)根…
Copyright ? 商夢(mèng)外包. All rights reserved.商夢(mèng)網(wǎng)校 版權(quán)所有 蘇ICP備14047127號(hào)-16 SiteMap