Scrapy概览
来源:互联网 发布:vpn端口号 编辑:程序博客网 时间:2024/06/13 18:20
引自:http://doc.scrapy.org/0.9/intro/overview.html
Scrape 是一个用于爬行Web站点、提取结构化数据的应用框架。它可应用于很多有用的应用场景,如数据挖掘,信息处理或历史档案等。
尽管Scrapy最初被设计用于screen scraping(更准确地说,是web scraping),它也能够被用来使用API抽取数据(比如Amazon Associates Web Services)或者作为通用的Web爬虫。
这篇文档的目的是向你介绍Scrapy中的一些概念,以便你能了解它是怎样工作的,并判断Scrapy是否能够满足你的需要。
当你准备好要开始一个项目时,你可以从Scrapy入门教程开始。
选择站点
假如你需要从某个Web站点上抽取某些信息,但是此Web站点并没有提供任何的API或其它机制使得你能够使用程序来获取所需要的信息,Scrapy可以帮助你。比如说,我们想提取 mininova 站点上今天添加的所有torrent文件的信息。
今天添加的所有torrent文件列表可以在 http://www.mininova.org/today 中找到。
写一个爬虫来提取Items
现在我们来写一个爬虫。在此爬虫中,将定义起始URL(http://www.mininova.org/today),下一步需要爬行的链接规则,以及从网页提取数据的规则。
如果仔细看一下网页的内容,可以发现所有torrent文件的URL都有如下的形式:http://www.mininova.org/tor/NUMBER,这里NUMBER是一个整数。我们可以利用这一点来为需要进一步爬行的链接构造正则表达式: /tor//d+。为了提取数据,我们使用 XPath来选择需要被提取的数据所在的文档部分。让我们看一下这些torrent网页中的某一个网页:
http://www.mininova.org/tor/2657665
察看一下这个网页的HTML源代码,然后构造XPath来选择我们要提取的数据,即:torrent name, description 和 size。
通过察看网页的HTML源代码,我们可以发现文件名字(file name)都嵌在<h1>标签中:
<h1>Home[2009][Eng]XviD-ovd</h1>
提取name的XPath表达式可以写成:
//h1/text()
而 description 嵌在<div>标签中,具有属性id="description":
<h2>Description:</h2>
<div id="description">
"HOME" - a documentary film by Yann Arthus-Bertrand
<br/>
<br/>
***
<br/>
<br/>
"We are living in exceptional times. Scientists tell us that we have 10 years to change the way we live, avert the depletion of natural resources and the catastrophic evolution of the Earth's climate.
...
用于选择description的XPath表达式可以写成:
//div[@id='description']
最后,file size嵌在<div>标签中的第二个<p>标签中,具有属性id=specifications
<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>
699.79 megabyte</p>
用于选择description的XPath表达式可以写成:
//div[@id='specifications']/p[2]/text()[2]
关于更多 XPath信息参见 XPath reference。
最后,爬虫的代码如下:
class MininovaSpider(CrawlSpider):
name = 'mininova.org'
allowed_domains = ['mininova.org']
start_urls = ['http://www.mininova.org/today']
rules = [Rule(SgmlLinkExtractor(allow=['/tor//d+']), 'parse_torrent')]
def parse_torrent(self, response):
x = HtmlXPathSelector(response)
torrent = TorrentItem()
torrent['url'] = response.url
torrent['name'] = x.select("//h1/text()").extract()
torrent['description'] = x.select("//div[@id='description']").extract()
torrent['size'] = x.select("//div[@id='info-left']/p[2]/text()[2]").extract()
return torrent
为简单起见, 这里故意省略了import语句和Torrent类的定义。
写存储提取出的Item的管道
现在,我们写一个Item Pipeline,其使用 pickle将提取出的Item序列化和存储到文件中。
import pickle
class StoreItemPipeline(object):
def process_item(self, spider, item):
torrent_id = item['url'].split('/')[-1]
f = open("torrent-%s.pickle" % torrent_id, "w")
pickle.dump(item, f)
f.close()
其它特性
你已经看到如何使用Scrapy来提取和存储一个Web站点上的Item,但是,这只是一个表面的东西。Scrapy提供了很多功能很强的特征使得scraping非常容易,也很有效率。如:
接下来要做的事情
很明显,接下来需要做的事情就是下载Scrapy,阅读入门教程和加入社区。
译者:赵传钢 email:chgzhao2006@126.com
- Scrapy概览
- Scrapy架构概览
- Scrapy设置之Performance概览
- scrapy源码分析(一)---------------------总执行流程概览
- 概览
- scrapy
- Scrapy
- scrapy
- Scrapy
- Scrapy
- Scrapy
- Scrapy
- Scrapy
- scrapy
- Scrapy
- scrapy
- Scrapy
- Scrapy
- 通过java查询数据库表结构
- OCP认证考试指南(13):管理共享服务器
- 如何重装dedeCMS
- C++字符串拷贝函数
- Tips: linux进程和线程差异(0)
- Scrapy概览
- oralce session 无法kill
- 验证不能为空的js
- Linux 用户(user)和用户组(group)管理概述
- oracle错误说明
- 2010 最牛B的个性签名
- OCP认证考试指南(14):管理数据库性能(1)
- 网站优化技术全攻略介绍
- ASP.NET环境的配置