python根據(jù)京東商品url獲取產(chǎn)品價(jià)格
京東商品詳細(xì)的請(qǐng)求處理,是先顯示html,然后再ajax請(qǐng)求處理顯示價(jià)格。
1.可以運(yùn)行js,并解析之后得到的html
2.模擬js請(qǐng)求,得到價(jià)格
# -*- coding: utf-8 -*- """ 根據(jù)京東url地址,獲取商品價(jià)格 京東請(qǐng)求處理過程,先顯示html頁面,然后通過ajax get請(qǐng)求獲取相應(yīng)的商品價(jià)格 1.商品的具體數(shù)據(jù)在html中的格式,如下(示例) # product: { # skuid: 1310118868, # name: '\u9999\u5f71\u77ed\u88d9\u4e24\u4ef6\u5957\u88c5\u5973\u0032\u0030\u0031\u0034\u51ac\u88c5\u65b0\u6b3e\u97e9\u7248\u957f\u8896\u0054\u6064\u4e0a\u8863\u8377\u53f6\u8fb9\u534a\u8eab\u88d9\u6f6e\u0020\u85cf\u9752\u0020\u004d', # skuidkey:'7781F505B71CE37A3AFBADA119D3587F', # href: 'http://item.jd.com/1310118868.html', # src: 'jfs/t385/197/414081450/336886/3070537b/541be890N2995990c.jpg', # cat: [1315,1343,1355], # brand: 18247, # nBrand: 18247, # tips: false, # type: 2, # venderId:38824, # shopId:'36786', # TJ:'0', # specialAttrs:["is7ToReturn-1"], # videoPath:'', # HM:'0' # } 2.ajax請(qǐng)求代碼如下: # // 獲得數(shù)字價(jià)格 # var getPriceNum = function(skus, $wrap, perfix, callback) { # skus = typeof skus === 'string' ? [skus]: skus; # $wrap = $wrap || $('body'); # perfix = perfix || 'J-p-'; # $.ajax({ # url: 'http://p.3.cn/prices/mgets?skuIds=J_' + skus.join(',J_') + '&type=1', # dataType: 'jsonp', # success: function (r) { # if (!r && !r.length) { # return false; # } # for (var i = 0; i < r.length; i++) { # var sku = r[i].id.replace('J_', ''); # var price = parseFloat(r[i].p, 10); # # if (price > 0) { # $wrap.find('.'+ perfix + sku).html('¥' + r[i].p + ''); # } else { # $wrap.find('.'+ perfix + sku).html('暫無報(bào)價(jià)'); # } # # if ( typeof callback === 'function' ) { # callback(sku, price, r); # } # } # } # }); # }; """ import urllib import json import re class JdPrice(object): """ 對(duì)獲取京東商品價(jià)格進(jìn)行簡(jiǎn)單封裝 """ def __init__(self, url): self.url = url self._response = urllib.urlopen(self.url) self.html = self._response.read() def get_product(self): """ 獲取html中,商品的描述(未對(duì)數(shù)據(jù)進(jìn)行詳細(xì)處理,粗略的返回str類型) :return: """ product_re = re.compile(r'compatible: true,(.*?)};', re.S) product_info = re.findall(product_re, self.html)[0] return product_info def get_product_skuid(self): """ 通過獲取的商品信息,獲取商品的skuid :return: """ product_info = self.get_product() skuid_re = re.compile(r'skuid: (.*?),') skuid = re.findall(skuid_re, product_info)[0] return skuid def get_product_name(self): pass def get_product_price(self): """ 根據(jù)商品的skuid信息,請(qǐng)求獲得商品price :return: """ price = None skuid = self.get_product_skuid() url = 'http://p.3.cn/prices/mgets?skuIds=J_' + skuid + '&type=1' price_json = json.load(urllib.urlopen(url))[0] if price_json['p']: price = price_json['p'] return price # 測(cè)試代碼 if __name__ == '__main__': url = 'http://item.jd.com/1310118868.html' url = 'http://item.jd.com/1044773.html' jp = JdPrice(url) print jp.get_product_price() # htm.decode('gb2312', 'ignore').encode('utf-8') # f = open('jjs.html', 'w') # f.write(htm) # f.close()
再給大家分享一個(gè)京東價(jià)格的爬蟲:
fromcreepyimportCrawler fromBeautifulSoupimportBeautifulSoup importurllib2 importjson classMyCrawler(Crawler): defprocess_document(self,doc): ifdoc.status==200: print[%d]%s%(doc.status,doc.url) try: soup=BeautifulSoup(doc.text.decode(gb18030).encode(utf-8)) exceptExceptionase: printe soup=BeautifulSoup(doc.text) printsoup.find(id="product-intro").div.h1.text url_id=urllib2.unquote(doc.url).decode(utf8).split(/)[-1].split(.)[0] f=urllib2.urlopen(http://p.3.cn/prices/get?skuid=J_+url_id,timeout=5) price=json.loads(f.read()) f.close() printprice[0][p] else: pass crawler=MyCrawler() crawler.set_follow_mode(Crawler.F_SAME_HOST) crawler.set_concurrency_level(16) crawler.add_url_filter(.(jpg|jpeg|gif|png|js|css|swf)$) crawler.crawl(http://item.jd.com/982040.html)
相關(guān)文章
python深度學(xué)習(xí)tensorflow卷積層示例教程
這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow卷積層示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06基于python實(shí)現(xiàn)的百度音樂下載器python pyqt改進(jìn)版(附代碼)
這篇文章主要介紹了基于python實(shí)現(xiàn)的百度音樂下載器python pyqt改進(jìn)版(附代碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Pyside2中嵌入Matplotlib的繪圖的實(shí)現(xiàn)
這篇文章主要介紹了Pyside2中嵌入Matplotlib的繪圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python參數(shù)解析模塊sys、getopt、argparse使用與對(duì)比分析
今天小編就為大家分享一篇關(guān)于Python參數(shù)解析模塊sys、getopt、argparse使用與對(duì)比分析,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04python使用paramiko模塊通過ssh2協(xié)議對(duì)交換機(jī)進(jìn)行配置的方法
今天小編就為大家分享一篇python使用paramiko模塊通過ssh2協(xié)議對(duì)交換機(jī)進(jìn)行配置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07關(guān)于Python中兩個(gè)不同shape的數(shù)組間運(yùn)算規(guī)則
這篇文章主要介紹了關(guān)于Python中兩個(gè)不同shape的數(shù)組間運(yùn)算規(guī)則,眾所周知,相同?shape?的兩個(gè)數(shù)組間運(yùn)算是指兩個(gè)數(shù)組的對(duì)應(yīng)元素相加,我們經(jīng)常會(huì)碰到一些不同?shape?的數(shù)組間運(yùn)算,需要的朋友可以參考下2023-08-08簡(jiǎn)單了解python高階函數(shù)map/reduce
這篇文章主要介紹了簡(jiǎn)單了解python高階函數(shù)map/reduce,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-06-06Python實(shí)現(xiàn)的登錄驗(yàn)證系統(tǒng)完整案例【基于搭建的MVC框架】
這篇文章主要介紹了Python實(shí)現(xiàn)的登錄驗(yàn)證系統(tǒng),結(jié)合完整實(shí)例形式分析了Python基于搭建的MVC框架進(jìn)行登錄驗(yàn)證操作的相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2019-04-04