当前位置:   article > 正文

爬虫案例—京东数据爬取、数据处理及数据可视化(效果+代码)_爬取京东手机销售数据python爬虫 数据可视分析

爬取京东手机销售数据python爬虫 数据可视分析

一、数据获取

        使用PyCharm(引用requests库、lxml库、json库、time库、openpyxl库和pymysql库)爬取京东网页相关数据(品牌、标题、价格、店铺等)

数据展示(片段):

        京东网页有反爬措施,需要自己在网页登录后,获取cookie,加到请求的header中(必要时引入time库,设置爬取睡眠时间降低封号概率)

爬取代码(片段):

  1. ###获取每一页的商品数据
  2. def getlist(url,brand):
  3. global count #定义一个全局变量,主要是为了确定写入第几行
  4. # url="https://search.jd.com/search?keyword=笔记本&wq=笔记本&ev=exbrand_联想%5E&page=9&s=241&click=1"
  5. res = requests.get(url,headers=headers)
  6. res.encoding = 'utf-8'
  7. # text = (res.text).replace("")
  8. text = res.text
  9. selector = etree.HTML(text)
  10. list = selector.xpath('//*[@id="J_goodsList"]/ul/li')#获取数据所在
  11. for i in list:
  12. title = i.xpath('.//div[@class="p-name p-name-type-2"]/a/em/text()')[0].strip()#商品名称
  13. price = i.xpath('.//div[@class="p-price"]/strong/i/text()')[0]#商品价格
  14. shop = i.xpath('.//div[@class="p-shop"]/span/a/text()')[0] #获取店铺名称
  15. #获取评论数的id值
  16. # product_id = i.xpath('.//[@class="p-commit"]/strong/a/@id')[0].replace("J_comment_","")
  17. # comment_count = commentcount(product_id)
  18. # print("目前条数="+str(count))
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号