欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python抓取京东价格分析京东商品价格走势

程序员文章站 2022-05-21 08:43:18
复制代码 代码如下:from creepy import crawlerfrom beautifulsoup import beautifulsoupimport urll...

复制代码 代码如下:

from creepy import crawler
from beautifulsoup import beautifulsoup
import urllib2
import json

class mycrawler(crawler):
    def process_document(self, doc):
        if doc.status == 200:
            print '[%d] %s' % (doc.status, doc.url)
            try:
                soup = beautifulsoup(doc.text.decode('gb18030').encode('utf-8'))
            except exception as e:
                print e
                soup = beautifulsoup(doc.text)
            print soup.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()
            print price[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')