教你用python爬取『京東』商品數(shù)據(jù),原來這么簡單!
↑ 關(guān)注 + 星標(biāo) ,每天學(xué)Python新技能
↑ 關(guān)注 + 星標(biāo) ,每天學(xué)Python新技能
后臺回復(fù)【大禮包】送你Python自學(xué)大禮包
后臺回復(fù)【大禮包】送你Python自學(xué)大禮包

今日三條文章教大家如何學(xué)會分析『京東』商城商品數(shù)據(jù)。

今日三條文章教大家如何學(xué)會分析『京東』商城商品數(shù)據(jù)。
1
前言
本文將從小白的角度入手,一步一步教大家如何爬取『京東』商品數(shù)據(jù),文中以【筆記本】電腦為例!
干貨內(nèi)容包括:
如何爬取商品信息?
如何爬取下一頁?
如何將爬取出來的內(nèi)容保存到excel?
2
分析網(wǎng)頁結(jié)構(gòu)
1.查看網(wǎng)頁
在『京東商城』搜索框輸入:筆記本


鏈接如下:
https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯(lián)想%5E&page=9&s=241&click=1在瀏覽器里面按F12,分析網(wǎng)頁標(biāo)簽(這里我們需要爬取1.商品名稱、2.商品價格、3.商品評論數(shù))
2.分析網(wǎng)頁標(biāo)簽
獲取當(dāng)前網(wǎng)頁所有商品

可以看到在class標(biāo)簽id=J_goodsList里ul->li,對應(yīng)著所有商品列表
獲取商品具體屬性

每一個li(商品)標(biāo)簽中,class=p-name p-name-type-2對應(yīng)商品標(biāo)題,class=p-price對應(yīng)商品價格,class=p-commit對應(yīng)商品ID(方便后面獲取評論數(shù))
避坑:
這里商品評論數(shù)不能直接在網(wǎng)頁上獲取?。。?,需要根據(jù)商品ID去獲取。
3
爬取數(shù)據(jù)
1.編程實現(xiàn)
url="https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯(lián)想%5E&page=9&s=241&click=1"res = requests.get(url,headers=headers)res.encoding = 'utf-8'text = res.textselector = etree.HTML(text)list = selector.xpath('//*[@id="J_goodsList"]/ul/li')for i in list:title=i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0]price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0]product_id = i.xpath('.//div[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","")print("title"+str(title))print("price="+str(price))print("product_id="+str(product_id))print("-----")

下面教大家如何獲取商品評論數(shù)!
2.獲取商品評論數(shù)
查看network,找到如下數(shù)據(jù)包


將該url鏈接放到瀏覽器里面可以獲取到商品評論數(shù)

分析url

根據(jù)商品ID(可以同時多個ID一起獲?。┇@取商品評論數(shù)

最后我們可以將獲取商品評論數(shù)的方法封裝成一個函數(shù)
def commentcount(product_id):url = "https://club.jd.com/comment/productCommentSummaries.action?referenceIds="+str(product_id)+"&callback=jQuery8827474&_=1615298058081"res = requests.get(url, headers=headers)res.encoding = 'gbk'text = (res.text).replace("jQuery8827474(","").replace(");","")text = json.loads(text)comment_count = text['CommentsCount'][0]['CommentCountStr']comment_count = comment_count.replace("+", "")if "萬" in comment_count:comment_count = comment_count.replace("萬","")comment_count = str(int(comment_count)*10000)return comment_count
此外,我們可以發(fā)現(xiàn)在獲取到的評論數(shù)包含“萬”“+”等符號,需要進(jìn)行相應(yīng)處理!
for i in list:title=i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0]price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0]product_id = i.xpath('.//div[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","")###獲取商品評論數(shù)comment_count = commentcount(product_id)print("title"+str(title))print("price="+str(price))print("product_id="+str(comment_count))

4
保存到excel
1.定義表頭
import openpyxloutwb = openpyxl.Workbook()outws = outwb.create_sheet(index=0)outws.cell(row=1,column=1,value="index")outws.cell(row=1,column=2,value="title")outws.cell(row=1,column=3,value="price")outws.cell(row=1,column=4,value="CommentCount")
引入openpyxl庫將數(shù)據(jù)保存到excel,表頭內(nèi)容包含(1.序號index、2.商品名稱title、3.商品價格price、4.評論數(shù)CommentCount)
2.開始寫入
for i in list:title=i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0]price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0]product_id = i.xpath('.//div[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","")###獲取商品評論數(shù)comment_count = commentcount(product_id)print("title"+str(title))print("price="+str(price))print("comment_count="+str(comment_count))outws.cell(row=count, column=1, value=str(count-1))outws.cell(row=count, column=2, value=str(title))outws.cell(row=count, column=3, value=str(price))outws.cell(row=count, column=4, value=str(comment_count))outwb.save("京東商品-李運(yùn)辰.xls")#保存
最后保存成京東商品-李運(yùn)辰.xls

5
下一頁分析
很重要!很重要!很重要!
1.分析下一頁
這里的下一頁與平常看到的不一樣,有點特殊!



可以發(fā)現(xiàn)page和s有一下規(guī)律

page以2遞增,s以60遞增。
2.構(gòu)造下一頁鏈接
遍歷每一頁def getpage():page=1s = 1for i in range(1,6):print("page="+str(page)+",s="+str(s))url = "https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯(lián)想%5E&page="+str(page)+"&s="+str(s)+"&click=1"getlist(url)page = page+2s = s+60
這樣就可以爬取下一頁。
6
總結(jié)
1.入門爬蟲(京東商品數(shù)據(jù)為例)。
2.如何獲取網(wǎng)頁標(biāo)簽。
3.獲取『京東』商品評論數(shù)
4.如何通過python將數(shù)據(jù)保存到excel
5.分析構(gòu)造『京東』商品網(wǎng)頁下一頁鏈接
如果大家對本文代碼源碼感興趣,點擊下放名片關(guān)注后,回復(fù):京東商品 ,獲取完整代碼!
如果大家想加群學(xué)習(xí),后臺點擊:加群交流
推薦閱讀
掃碼回復(fù)「大禮包」后獲取大禮
