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抓取豆瓣影评数据的方法

使用Python抓取豆瓣影评数据的方法

抓取豆瓣影评评分 正常的抓取 分析请求的url https://movie.douban.com/subject/26322642/comments?start=20&limit=...

Python3使用正则表达式爬取内涵段子示例

Python3使用正则表达式爬取内涵段子示例

本文实例讲述了Python3使用正则表达式爬取内涵段子的方法。分享给大家供大家参考,具体如下: 似乎正则在爬虫中用的不是很广泛,但是也是基本功需要我们去掌握。 先将内涵段子网页爬取下来,...

使用python BeautifulSoup库抓取58手机维修信息

直接上代码: 复制代码 代码如下:#!/usr/bin/python# -*- coding: utf-8 -*- import urllib import os,datetime,st...

详解用python写网络爬虫-爬取新浪微博评论

新浪微博需要登录才能爬取,这里使用m.weibo.cn这个移动端网站即可实现简化操作,用这个访问可以直接得到的微博id。 分析新浪微博的评论获取方式得知,其采用动态加载。所以使用json...

python爬取淘宝商品详情页数据

python爬取淘宝商品详情页数据

在讲爬取淘宝详情页数据之前,先来介绍一款 Chrome 插件:Toggle JavaScript (它可以选择让网页是否显示 js 动态加载的内容),如下图所示: 当这个插件处于关闭状...