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

相关文章

Python3网络爬虫中的requests高级用法详解

Python3网络爬虫中的requests高级用法详解

本节我们再来了解下 Requests 的一些高级用法,如文件上传,代理设置,Cookies 设置等等。 1. 文件上传 我们知道 Reqeuests 可以模拟提交一些数据,假如有的网站需...

Python实现抓取HTML网页并以PDF文件形式保存的方法

本文实例讲述了Python实现抓取HTML网页并以PDF文件形式保存的方法。分享给大家供大家参考,具体如下: 一、前言 今天介绍将HTML网页抓取下来,然后以PDF保存,废话不多说直接进...

python抓取需要扫微信登陆页面

python抓取需要扫微信登陆页面

  一,抓取情况描述 1.抓取的页面需要登陆,以公司网页为例,登陆网址https://app-ticketsys.hezongyun.com/index.php ,(该网页登...

Python3多线程爬虫实例讲解代码

多线程概述 多线程使得程序内部可以分出多个线程来做多件事情,充分利用CPU空闲时间,提升处理效率。python提供了两个模块来实现多线程thread 和threading ,thread...

python抓取百度首页的方法

本文实例讲述了python抓取百度首页的方法。分享给大家供大家参考。具体实现方法如下: import urllib def downURL(url,filename): try:...