前言
今天給大家介紹的是Python爬取手機商品資訊資料,在這里給需要的小伙伴們代碼,并且給出一點小心得,
首先是爬取之前應該盡可能偽裝成瀏覽器而不被識別出來是爬蟲,基本的是加請求頭,但是這樣的純文本資料爬取的人會很多,所以我們需要考慮更換代理IP和隨機更換請求頭的方式來對手機資訊資料進行爬取,
在每次進行爬蟲代碼的撰寫之前,我們的第一步也是最重要的一步就是分析我們的網頁,
通過分析我們發現在爬取程序中速度比較慢,所以我們還可以通過禁用谷歌瀏覽器圖片、JavaScript等方式提升爬蟲爬取速度,

開發工具
Python版本: 3.6
相關模塊:
requests模塊
json模塊
lxml模塊
openpyxl
環境搭建
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
文中完整代碼及Excel檔案,評論留言獲取
思路分析
瀏覽器中打開我們要爬取的頁面
按F12進入開發者工具,查看我們想要的手機商品資料在哪里
這里我們需要頁面資料就可以了

代碼實作
請求頭防止反爬
# 這里提示不用請求也是可以的只保留user-agent也可以爬取資料
headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.
100 Safari/537.36',
'cookie':'你的Cookie',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'upgrade-insecure-requests': '1',
'referer': 'https://www.jd.com/',
}
獲取商品評論數
import openpyxl
outwb = openpyxl.Workbook()
outws = outwb.create_sheet(index=0)
outws.cell(row=1,column=1,value="https://www.cnblogs.com/guzichuan/archive/2022/12/12/index")
outws.cell(row=1,column=2,value="https://www.cnblogs.com/guzichuan/archive/2022/12/12/title")
outws.cell(row=1,column=3,value="https://www.cnblogs.com/guzichuan/archive/2022/12/12/price")
outws.cell(row=1,column=4,value="https://www.cnblogs.com/guzichuan/archive/2022/12/12/CommentCount")
count=2
根據商品id獲取評論數
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
獲取每一頁的商品資料
def getlist(url):
global count
#url="https://search.jd.com/search?keyword=筆記本&wq=筆記本&ev=exbrand_聯想%5E&page=9&s=241&click=1"
res = requests.get(url,headers=headers)
res.encoding = 'utf-8'
text = res.text
selector = etree.HTML(text)
list = selector.xpath('//*[@id="J_goodsList"]/ul/li')
for i in list:
title=i.xpath('.//div[@]/a/em/text()')[0]
price = i.xpath('.//div[@]/strong/i/text()')[0]
product_id = i.xpath('.//div[@]/strong/a/@id')[0].replace("J_comment_","")
comment_count = commentcount(product_id)
#print(title)
#print(price)
#print(comment_count)
outws.cell(row=count, column=1, value=https://www.cnblogs.com/guzichuan/archive/2022/12/12/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))
count = count +1
#print("-----")
遍歷每一頁
def getpage():
page=1
s = 1
for i in range(1,6):
print("page="+str(page)+",s="+str(s))
url = "https://search.jd.com/Search?keyword=手機=utf-8&wq=手機=56b2bc7c47db4861986201bb72c1b281"+str(page)+"&s="+str(s)+"&click=1"
getlist(url)
page = page+2
s = s+60
結果展示

最后
今天的分享到這里就結束了 ,感興趣的朋友也可以去試試哈
對文章有問題的,或者有其他關于python的問題,可以在評論區留言或者私信我哦
覺得我分享的文章不錯的話,可以關注一下我,或者給文章點贊(/≧▽≦)/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/539752.html
標籤:其他
上一篇:網路編程初識
