本文介紹兩種抓取價格的方法
1瓤介、讀取接口獲取價格 scrapy等等么抗。苹祟。簡單
2砸抛、基于模擬瀏覽器讀取頁面抓取selenium 簡單
京東商品價格目前是基于api接口獲取然后通過js進行數(shù)值初始化
一、基于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格式的數(shù)據(jù)如下:
其中p對應的就是商品價格
2、爬取評論
http://club.jd.com/productpage/p-12608054023-s-0-t-3-p-0.html
其中12608054023是商品的id
返回的是個json格式的數(shù)據(jù)
其中有商品數(shù)量和評價信息
scrapy shell https://p.3.cn/prices/get\?skuid\=J_11896401
import json
data = json.loads(response.body)
print data[0].get('p')