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

1

前言

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

干货内容包括:

  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.分析构造『京东』商品网页下一页链接

  • 11
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
当然可以,以下是一个简单Python程序,可以使用 BeautifulSoup 和 requests 库来爬取京东商品数据: ```python import requests from bs4 import BeautifulSoup # 设置请求头信息 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} # 输入要搜索的商品名称 keyword = input("请输入商品名称:") # 构造请求URL url = "https://search.jd.com/Search?keyword=" + keyword # 发送请求并获取响应内容 response = requests.get(url, headers=headers) # 使用 BeautifulSoup 解析响应内容 soup = BeautifulSoup(response.text, "html.parser") # 获取商品列表 goods_list = soup.select("#J_goodsList > ul > li") # 遍历商品列表并输出商品的名称、价格和链接 for goods in goods_list: name = goods.select(".p-name em")[0].text.strip() price = goods.select(".p-price i")[0].text link = "https:" + goods.select(".p-name a")[0]["href"] print("商品名称:", name) print("商品价格:", price) print("商品链接:", link) print("----------------------") ``` 在这段代码中,我们首先设置请求头信息,以便能够成功发送请求。然后我们让用户输入要搜索的商品名称,使用该名称构造请求URL,并发送请求。接下来,我们使用 BeautifulSoup 解析响应内容,获取商品列表,并遍历商品列表,输出商品的名称、价格和链接。 请注意,这只是一个简单的示例程序,它只能获取一页搜索结果。如果您想要获取更多的结果,您需要编写更复杂的代码来处理分页。另外,请注意,京东的网站结构可能会发生变化,因此这段代码可能无法长期运行,您需要根据实际情况进行调整。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值