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

yipeiwu_com5年前Python爬虫

复制代码 代码如下:

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')

相关文章

python用BeautifulSoup库简单爬虫实例分析

会用到的功能的简单介绍 1、from bs4 import BeautifulSoup #导入库 2、请求头herders headers={'User-Agent': 'Mo...

python打造爬虫代理池过程解析

最近在使用爬虫爬取数据时,经常会返回403代码,大致意思是该IP访问过于频繁,被限制访问。限制IP访问网站最常用的反爬手段了,其实破解也很容易,就是在爬取网站是使用代理即可,这个IP被限...

在Python中使用cookielib和urllib2配合PyQuery抓取网页信息

在Python中使用cookielib和urllib2配合PyQuery抓取网页信息

刚才好无聊,突然想起来之前做一个课表的点子,于是百度了起来。 刚开始,我是这样想的:在写微信墙的时候,用到了urllib2【两行代码抓网页】,那么就只剩下解析html了。于是百度:pyt...

Python使用urllib2模块抓取HTML页面资源的实例分享

先把要抓取的网络地址列在单独的list文件中 //www.jb51.net/article/83440.html //www.jb51.net/article/83437.html...

Python天气预报采集器实现代码(网页爬虫)

爬虫简单说来包括两个步骤:获得网页文本、过滤得到数据。   1、获得html文本。   python在获取html方面十分方便,寥寥数行代码就可以实现我们需要的功能。 复制代码 代码如下...