我们的爬虫从pyspider开始说起(一)
看各种爬虫文献也有好几天了,总是感觉下不了手,总结一句“提笔忘字,总是因为看的太多而写的太少”。所以从现在开始,把看到的想到的,需要总结的东西慢慢的都沉淀下来,扎扎实实的走好每一步。
先来说这几天遇到的各种坑,好不容易找到了关于pyquery和pyspider的各种资料,准备敲到电脑上试试,结果出现了好几个问题。电脑上安装的是python3,代码是python2,转换好了环境,发现目标网站的格式变得,各种苦恼,各种困惑。或许这也是很多学习写爬虫的人总会遇到的问题。从网上下载了几本书,对照着写,结果发现各种库的调用格式都变了,真的是各种坑,各种坑,坑。。。。。
先来看pyspider的官方docs中的第一个例子
from pyspider.libs.base_handler import * class Handler(BaseHandler):
crawl_config = {
} @every(minutes=24 * 60)
def on_start(self):
self.crawl('http://scrapy.org/', callback=self.index_page) #很逗比的pyspider的案例居然爬去另一位大神之作scrapy的网站,这一行可以看作是初始化,从callback函数中可以看出调用index_page @config(age=10 * 24 * 60 * 60)
def index_page(self, response):
for each in response.doc('a[href^="http"]').items(): # 用pyquery解析获取初始链接中的每一个超级链接,对每一个链接再调用detail_page,需要注意的是如果需要翻页的话,在翻页的链接上再调用index_page
self.crawl(each.attr.href, callback=self.detail_page) @config(priority=2)
def detail_page(self, response): #在detail_page页面中获取 url和title,并返回
return {
"url": response.url,
"title": response.doc('title').text(),
}
def on_start(self)
是爬虫的入口,当点击run时显示的第一个页面self.crawl(url, callback=self.index_page)
* 是程序最重要的接口。它添加了一个新的爬去任务。需要注意的是self.crawl
中有很多参数可以自己设置。def index_page(self, response)返回一个
Response
* 对象.response.doc
* 是 pyquery 对象,这个对象就像 jQuery的 API 一样获取元素。def detail_page(self, response)返回字典
对象. 这个结果默认由resultdb
获取. 可以通过改写on_result(self, result)方法处理存储方式。
@every(minutes=24*60, seconds=0)
* 24小时乘以60分整天运行@config(age=10 * 24 * 60 * 60)
* 运行10天age=10 * 24 * 60 * 60
* tell scheduler discard the request if it have been crawled in 10 days. pyspider will not crawl a same URL twice by default (discard forever), even you had modified the code, it's very common for beginners that runs the project the first time and modified it and run it the second time, it will not crawl again (readitag
for solution)@config(priority=2)
* detail pages 优先运行
官方docs中爬取 http://www.imdb.com的爬虫也因为IMDB的改版已经不能使用,顺手调试了一下代码
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2015-01-04 03:08:55
# Project: tutorial_imdb from pyspider.libs.base_handler import * class Handler(BaseHandler):
"""
It's a sample scrape script of pyspider tutorial: Level 1: HTML and CSS Selector
http://docs.pyspider.org/en/latest/tutorial/HTML-and-CSS-Selector/
""" @every(minutes=24 * 60)
def on_start(self):
self.crawl('http://www.imdb.com/search/title?count=100&title_type=feature,tv_series,tv_movie&ref_=nv_ch_mm_1', callback=self.index_page) @config(age=24 * 60 * 60)
def index_page(self, response):
for each in response.doc('a[href^="http"]').items():
for each in response.doc('h3.lister-item-header a').items(): 把官方文档中的正则表达式改成了css path,这样看起来容易理解多了
self.crawl(each.attr.href, priority=9, callback=self.detail_page)
self.crawl([x.attr.href for x in response.doc('.next-page').items()], callback=self.index_page) def detail_page(self, response):
return {
"url": response.url,
"title": response.doc('div.title_wrapper h1').text(),
"rating": response.doc('div.ratingValue strong span').text(),
"stars": [x.text() for x in response.doc('div.credit_summary_item span a span.itemprop').items()],
}
进一步分析IMDB网站就会发现,影片详细信息页面下面的很多是不能够用csspath抓取的,需要进一步解析,明天再来(*^_^*)
我们的爬虫从pyspider开始说起(一)的更多相关文章
- Python的两个爬虫框架PySpider与Scrapy安装
Python的两个爬虫框架PySpider与Scrapy安装 win10安装pyspider: 最好以管理员身份运行CMD,不然可能会出现拒绝访问文件夹的情况! pyspider:pip instal ...
- Python爬虫之PySpider框架
概述 pyspider 是一个支持任务监控.项目管理.多种数据库,具有 WebUI 的爬虫框架,它采用 Python 语言编写,分布式架构.详细特性如下: 拥有 Web 脚本编辑界面,任务监控器,项目 ...
- [转]Python爬虫框架--pyspider初体验
标签: python爬虫pyspider 2015-09-05 10:57 9752人阅读 评论(0) 收藏 举报 分类: Python(8) 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- 爬虫之pyspider
1.简单的介绍 pyspider是由国人binux编写的强大的网络爬虫系统,其GitHub地址为 https://github.com/binux/pyspider 官方文档地址为 http://do ...
- 爬虫框架pyspider的使用
j概要:了解了爬虫的基础知识后,接下来我们来使用框架来写爬虫,用框架会使我们写爬虫更加简单,接下来我们来了解一下,pyspider框架的使用,了解了该框架,妈妈再也不用担心我们的学习了. 前期准备: ...
- Python爬虫之一 PySpider 抓取淘宝MM的个人信息和图片
ySpider 是一个非常方便并且功能强大的爬虫框架,支持多线程爬取.JS动态解析,提供了可操作界面.出错重试.定时爬取等等的功能,使用非常人性化. 本篇通过做一个PySpider 项目,来理解 Py ...
- Python爬虫框架--pyspider初体验
之前接触scrapy本来是想也许scrapy能够让我的爬虫更快,但是也许是我没有掌握scrapy的要领,所以爬虫运行起来并没有我想象的那么快,看这篇文章就是之前使用scrapy的写得爬虫.然后昨天我又 ...
- 爬虫之pyspider 安装
解决方法: 利用wheel安装 S1: pip install wheelS2: 进入www.lfd.uci.edu/~gohlke/pythonlibs/,Ctrl + F查找pycurl S3:这 ...
- python爬虫框架Pyspider初次接触
pyspider网站地址:http://docs.pyspider.org/en/latest/.文档比较好,安装起来也非常方便.既然是基于python的框架,那么首先得安装python.微软出的一款 ...
随机推荐
- 微信小程序创建一个新项目
1. 新建一个文件夹. 2. 打开微信小程序开发工具,导入新建文件夹:然后输入创建的appId:会自动生成一个project.config.json,打开这个文件,会看到appid这个字段. 3.可以 ...
- dubbo协议报文格式
- nginx多域名、多证书
环境: 一台nginx服务器 192.168.10.251 两台windowsserver2012 IIS服务器 (192.168.10.252.192.168.10.253) 从阿里云上下载ssl证 ...
- mysql学习笔记--数据库预处理
一.概念 1. 预编译一次,可以多次执行.用来解决一条sql语句频繁执行的问题 2. 语法 a. 预处理语句:preapre 预处理名字 from 'sql语句' b. 执行预处理:execute 预 ...
- POJ-2236.WireleseNetwork.(并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 43199 Accepted: 178 ...
- centos 7 开机yum
yum -y install net-tools yum -y install wget
- Cisco交换机设置备份
conf tusername xa privilege 3 secret xxx aaa new-modelaaa authentication login default local enablea ...
- Ubuntu 16.04 上安装 PCL 1.8.0
Ubuntu16.04之后安装pcl可以直接apt-get sudo apt-get install libpcl-dev pcl-tools 安装之前,准备一些依赖库 sudo apt-get up ...
- sqlserver改主键初始ID
- python基础(16)私有类,类,类变量
1.私有类: class My: def test(self): self.__password = 123456 def say(self): print('password',self.__pas ...