您的位置:首页 > 其它

Scrapy 0.22 文档翻译 之 Scrapy一瞥

2014-02-23 18:09 127 查看
选择一个网站

假设我们要从Mininova网站 中提取所有今天添加的文件的url,name,description和size

网址为 http://www.mininova.org/today
定义你要抓取的字段

第一件事是要定义字段,在Scrapy里是通过Scrapy Items来做这件事

以下是我们的项目

from scrapy.item import Item, Field

class TorrentItem(Item):
url = Field()
name = Field()
description = Field()
size = Field()
写一个爬虫提取数据

接下来的是写一个爬虫文件定义起始url( http://www.mininova.org/today) ,定义提取新的url链接和提取所需数据的规则.

如果我们查看起始url的html源代码中文件列表的链接,我们会发现一个规律,所有文件url类似于http://www.mininova.org/tor/+数字,我们可以用正则表达式 "/tor/\d+" 来提取所有文件的url地址.

我们使用Xpath语法来从html源代码中提取数据,我们打开其中一个文件的详情页,比如 http://www.mininova.org/tor/2676093 

查看详情页的源代码,构建一个Xpath去选择我们需要的数据,name, description 和size.

我们可以发现name在h1标签里

<h1>Darwin - The Evolution Of An Exhibition</h1>它的Xpath表达式为: //h1/text()
description在id="description"的div标签里

<h2>Description:</h2>

<div id="description">
Short documentary made for Plymouth City Museum and Art Gallery regarding the setup of an exhibit about Charles Darwin in conjunction with the 200th anniversary of his birth.

...它的Xpath可以这样写:  //div[@id='description']

最后是size,它在id="specifications"的div标签中的第2个p标签里
<div id="specifications">

<p>
<strong>Category:</strong>
<a href="/cat/4">Movies</a> > <a href="/sub/35">Documentary</a>
</p>

<p>
<strong>Total size:</strong>
150.62 megabyte</p>

它的Xpath语法是这样: //div[@id='specifications']/p[2]/text()[2]
获取更多Xpath信息请点击这里 XPath reference

最后,爬虫的代码如下:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector

class MininovaSpider(CrawlSpider):

name = 'mininova'
allowed_domains = ['mininova.org']
start_urls = ['http://www.mininova.org/today']
rules = [Rule(SgmlLinkExtractor(allow=['/tor/\d+']), 'parse_torrent')]

def parse_torrent(self, response):
sel = Selector(response)
torrent = TorrentItem()
torrent['url'] = response.url
torrent['name'] = sel.xpath("//h1/text()").extract()
torrent['description'] = sel.xpath("//div[@id='description']").extract()
torrent['size'] = sel.xpath("//div[@id='info-left']/p[2]/text()[2]").extract()
return torrent

执行爬虫去提取数据

最终我们会执行爬虫从网站抓取数据,并以json格式保存到scraped_data.json文件中

scrapy crawl mininova -o scraped_data.json -t json


这里,我们使用Feed exports
来生成json文件,你也可以很方便的修改成其它格式(比如xml,csv),也可以通过后端存储(例如FTP或Amazon S3)

我们也可以很轻易地写一个 item pipeline 把数据存到数据库中

回顾抓取到的数据

如果你在抓取进程结束后,检查scraped_data.json文件,你会看到抓取的items如下:

[{"url": "http://www.mininova.org/tor/2676093", "name": ["Darwin - The Evolution Of An Exhibition"], "description": ["Short documentary made for Plymouth ..."], "size": ["150.62 megabyte"]},
# ... other items ...
]

你会发现除了url(因为是直接赋值)外,其它字段都是一个列表(lists) , 这是因为 Selectors
返回的是一个列表(lists) ,也许你想存储单个的值,或者为它执行附加的解析
(parsing)/过滤(cleansing) , 这是

item Loaders 所做的事情.

还有什么?



你已经看到如何用Scrapy从一个网站上提取并保存items,但这只是表面.

 Scrapy提供许多强大的特性,为使抓取变得简单而有效,例如:

内置支持从HTML和XML中选择并提取数据
内置支持清除和过滤抓取到的数据,通过一个可重复使用的集合(item loaders ),并在所有爬虫中共享
内置支持生成并导出多种格式的文件(JSON,CSV,XML),或者通过多种后台存储(FTP, S3, local filesystem)
media pipeline 会自动地下载相关的图片或其它媒体文件通过抓取的items

支持扩展Scrapy,加入自己的功能通过信号(signals)和定义好的API(middlewares, extensions, and pipelines)
广泛的内置中间件和扩展cookies and session handling,HTTP compression,HTTP authentication,HTTP cache, user-agent
spoofing,robots.txt等<
92b6
/li>强健的编码支持和自动检测
支持在预定义模版基础上创建新的爬虫,加速开发,且在大项目上使代码保持一致
可扩展的 stats collection ,在监视性能和监测何时挂掉时非常有用
交互式的shell控制台,用来测试Xpath语法,对写和调试爬虫非常有用
一个旨在简化部署和运行爬虫的系统服务
一个内置的web-Service 来监视和控制运营
Telnet控制台挂钩到python控制台运行在你的Scrapy,可用于调试抓取
日志,可以记录程序运行中的错误信息
支持基于urls通过Sitempas抓取
DNS缓存

(由于水平有限,欢迎指正错误,谢谢)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: