教你用python爬取『京东』商品数据,原来这么简单!

Python绿色通道

共 4300字,需浏览 9分钟

 ·

2021-03-14 08:43

↑ 关注 + 星标 ,每天学Python新技能

后台回复【大礼包】送你Python自学大礼包

今日三条文章教大家如何学会分析『京东』商城商品数据


1

前言


本文将从小白的角度入手,一步一步教大家如何爬取『京东』商品数据,文中以【笔记本】电脑为例!


干货内容包括:

  1. 如何爬取商品信息?

  2. 如何爬取下一页?

  3. 如何将爬取出来的内容保存到excel?



2

分析网页结构


1.查看网页

京东商城搜索框输入:笔记本





链接如下:

https://search.jd.com/search?keyword=笔记本&wq=笔记本&ev=exbrand_联想%5E&page=9&s=241&click=1


在浏览器里面按F12,分析网页标签(这里我们需要爬取1.商品名称2.商品价格3.商品评论数


2.分析网页标签

获取当前网页所有商品




可以看到在class标签id=J_goodsList里ul->li,对应着所有商品列表


获取商品具体属性


每一个li(商品)标签中,class=p-name p-name-type-2对应商品标题,class=p-price对应商品价格,class=p-commit对应商品ID(方便后面获取评论数


避坑:

这里商品评论数不能直接在网页上获取!!!,需要根据商品ID去获取。



3

爬取数据


1.编程实现

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[@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("-----")




下面教大家如何获取商品评论数!

2.获取商品评论数

查看network,找到如下数据包




将该url链接放到浏览器里面可以获取到商品评论数



分析url



根据商品ID(可以同时多个ID一起获取)获取商品评论数


最后我们可以将获取商品评论数的方法封装成一个函数


###根据商品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


此外,我们可以发现在获取到的评论数包含“万”“+”等符号,需要进行相应处理!


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_","")            ###获取商品评论数    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库将数据保存到excel,表头内容包含(1.序号index、2.商品名称title、3.商品价格price、4.评论数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_","")

###获取商品评论数 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("京东商品-李运辰.xls")#保存


最后保存成京东商品-李运辰.xls




5

下一页分析


很重要!很重要!很重要!

1.分析下一页

这里的下一页与平常看到的不一样,有点特殊!





可以发现page和s有一下规律



page以2递增,s以60递增。

2.构造下一页链接

遍历每一页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=笔记本&wq=笔记本&ev=exbrand_联想%5E&page="+str(page)+"&s="+str(s)+"&click=1"        getlist(url)        page = page+2        s = s+60


这样就可以爬取下一页。


6

总结


1.入门爬虫(京东商品数据为例)。

2.如何获取网页标签。

3.获取『京东』商品评论数

4.如何通过python将数据保存到excel

5.分析构造『京东』商品网页下一页链接


如果大家对本文代码源码感兴趣,点击下放名片关注后,回复:京东商品 ,获取完整代码!



如果大家想加群学习,后台点击:加群交流


推荐阅读

  1. 工程师姓什么很重要!别再叫我“X工”!!!

  2. 这可能是你近2年进腾讯的最好机会!

  3. 稳了!已官宣:抄小道进腾讯的机会来了

扫码回复「大礼包」后获取大礼

浏览 114
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报