美文网首页
python抓取京东商品价格

python抓取京东商品价格

作者: 冰_茶 | 来源:发表于2017-07-06 18:14 被阅读0次

    本文介绍两种抓取价格的方法

    1、读取接口获取价格 scrapy等等。。简单

    2、基于模拟浏览器读取页面抓取selenium 简单

    京东商品价格目前是基于api接口获取然后通过js进行数值初始化

    一、基于selenium模拟浏览器进行抓取


    In [11]: from selenium import webdriver

    In [12]: drive = webdriver.PhantomJS()

    In [13]: driver.get("https://item.jd.com/12608054023.html")

    In [14]: driver.find_element_by_class_name("price").text

    Out[14]: u'119.00'

    In [15]: driver.find_element_by_class_name("p-price-plus").text

    Out[15]: u'\uffe5109.00'

    In [16]: print driver.find_element_by_class_name("p-price-plus").text

    ¥109.00

    In [17]: driver.find_element_by_class_name("p-price").text

    Out[17]: u'\uffe5119.00'

    In [18]: print driver.find_element_by_class_name("p-price").text

    导入到BeautifulSoup处理

    from bs4 import BeautifulSoup as bs

    pageSource = driver.page_source

    bsobj = bs(pageSource)

    print bsobj.find('span',{'class':'p-price'}).get_text()

    二、基于scrapy抓取方式

    1、爬取价格

    http://p.3.cn/prices/mgets?skuIds=J_12608054023,J_&type=1

    其中12608054023是商品的id

    返回的是个json格式的数据如下:

    其中p对应的就是商品价格

    2、爬取评论

    http://club.jd.com/productpage/p-12608054023-s-0-t-3-p-0.html

    其中12608054023是商品的id

    返回的是个json格式的数据

    其中有商品数量和评价信息

    scrapy shell https://p.3.cn/prices/get\?skuid\=J_11896401

    import json

    data = json.loads(response.body)

    print data[0].get('p')

    相关文章

      网友评论

          本文标题:python抓取京东商品价格

          本文链接:https://www.haomeiwen.com/subject/mrqkhxtx.html