Scrapy Tutorial¶
在本教程中,我们假设您的系统上已经安装了Scrapy. 如果不是这种情况,请参阅《 安装指南》 .
我们将删除quotes.toscrape.com ,该网站列出了著名作家的名言.
本教程将指导您完成以下任务:
- 创建一个新的Scrapy项目
- Writing a spider to crawl a site and extract data
- 使用命令行导出抓取的数据
- 更改蜘蛛以递归地跟随链接
- 使用蜘蛛参数
Scrapy用Python编写. 如果您是该语言的新手,则可能首先要了解该语言的外观,以充分利用Scrapy.
如果您已经熟悉其他语言,并且想快速学习Python,那么Python教程是一个很好的资源.
如果您不熟悉编程并且想开始使用Python,那么以下书籍可能对您有用:
- Automate the Boring Stuff With Python
- How To Think Like a Computer Scientist
- Learn Python 3 The Hard Way
You can also take a look at this list of Python resources for non-programmers, as well as the suggested resources in the learnpython-subreddit .
Creating a project¶
在开始抓取之前,您将必须设置一个新的Scrapy项目. 输入要存储代码并运行的目录:
scrapy startproject tutorial
这将创建一个包含以下内容的tutorial
目录:
tutorial/
scrapy.cfg # deploy configuration file
tutorial/ # project's Python module, you'll import your code from here
__init__.py
items.py # project items definition file
middlewares.py # project middlewares file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py
Our first Spider¶
蜘蛛是您定义的类,Scrapy用于从网站(或一组网站)中获取信息. 他们必须将Spider
子类化,并定义最初的请求,可以选择如何跟随页面中的链接,以及如何解析下载的页面内容以提取数据.
这是我们第一个Spider的代码. 将其保存在项目中tutorial/spiders
目录下的一个名为quotes_spider.py
的文件中:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'quotes-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.body)
self.log('Saved file %s' % filename)
如您所见,我们的Spider子类scrapy.Spider
并定义了一些属性和方法:
name
:标识蜘蛛. 它在一个项目中必须是唯一的,也就是说,不能为不同的Spider设置相同的名称.start_requests()
:必须返回一个可迭代的Requests(您可以返回请求列表或编写生成器函数),Spider将开始从中进行爬网. 随后的请求将从这些初始请求中依次生成.parse()
:将被调用以处理为每个请求下载的响应的方法. response参数是TextResponse
一个实例,该实例保存页面内容并具有其他有用的方法来处理它.
How to run our spider¶
To put our spider to work, go to the project’s top level directory and run:
scrapy crawl quotes
该命令运行带有我们刚刚添加的名称quotes
的蜘蛛,它将发送对quotes.toscrape.com
域的一些请求. 您将获得类似于以下的输出:
... (omitted for brevity)
2016-12-16 21:24:05 [scrapy.core.engine] INFO: Spider opened
2016-12-16 21:24:05 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-12-16 21:24:05 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-12-16 21:24:05 [scrapy.core.engine] DEBUG: Crawled (404) <GET http://quotes.toscrape.com/robots.txt> (referer: None)
2016-12-16 21:24:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None)
2016-12-16 21:24:05 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/2/> (referer: None)
2016-12-16 21:24:05 [quotes] DEBUG: Saved file quotes-1.html
2016-12-16 21:24:05 [quotes] DEBUG: Saved file quotes-2.html
2016-12-16 21:24:05 [scrapy.core.engine] INFO: Closing spider (finished)
...
现在,检查当前目录中的文件. 您应该注意到,已经创建了两个新文件: quotes-1.html和quotes-2.html ,其中包含相应URL的内容,如我们的parse
方法所指示的那样.
Note
如果您想知道为什么我们还没有解析HTML,请稍等,我们将尽快解决.
What just happened under the hood?¶
Scrapy调度Spider的start_requests
方法返回的scrapy.Request
对象. 在收到每个响应时,它实例化Response
对象并调用与请求关联的回调方法(在本例中为parse
方法),并将响应作为参数传递.
A shortcut to the start_requests method¶
无需实现从URL生成scrapy.Request
对象的start_requests()
方法,您只需定义带有URL列表的start_urls
类属性即可. 然后, start_requests()
的默认实现将使用此列表来为您的蜘蛛创建初始请求:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'quotes-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.body)
即使我们没有明确告诉Scrapy这样做,也会调用parse()
方法来处理这些URL的每个请求. 发生这种情况是因为parse()
是Scrapy的默认回调方法,对于没有显式分配的回调的请求将调用该方法.
Extracting data¶
学习如何使用Scrapy提取数据的最佳方法是使用Scrapy shell尝试选择器. 跑:
scrapy shell 'http://quotes.toscrape.com/page/1/'
Note
请记住,从命令行运行Scrapy shell时,请始终将网址括在引号中,否则包含参数(即&
字符)的网址将不起作用.
在Windows上,请使用双引号代替:
scrapy shell "http://quotes.toscrape.com/page/1/"
您将看到类似以下内容:
[ ... Scrapy log here ... ]
2016-09-19 12:09:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None)
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x7fa91d888c90>
[s] item {}
[s] request <GET http://quotes.toscrape.com/page/1/>
[s] response <200 http://quotes.toscrape.com/page/1/>
[s] settings <scrapy.settings.Settings object at 0x7fa91d888c10>
[s] spider <DefaultSpider 'default' at 0x7fa91c8af990>
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
>>>
使用外壳,您可以尝试使用带有响应对象的CSS选择元素:
>>> response.css('title')
[<Selector xpath='descendant-or-self::title' data='<title>Quotes to Scrape</title>'>]
运行response.css('title')
是一个名为SelectorList
的类似列表的对象,该对象表示围绕XML / HTML元素的Selector
对象的列表,并允许您运行进一步的查询来细化选择或提取内容数据.
要从上面的标题中提取文本,您可以执行以下操作:
>>> response.css('title::text').getall()
['Quotes to Scrape']
这里有两点要注意:一是我们在CSS查询中添加了::text
,这意味着我们只想直接在<title>
元素内选择text元素. 如果不指定::text
,则将获得完整的title元素,包括其标签:
>>> response.css('title').getall()
['<title>Quotes to Scrape</title>']
另一件事是,调用.getall()
的结果是一个列表:选择器有可能返回多个结果,因此我们将它们全部提取出来. 当您知道只想要第一个结果时,在这种情况下,您可以执行以下操作:
>>> response.css('title::text').get()
'Quotes to Scrape'
或者,您可以编写:
>>> response.css('title::text')[0].get()
'Quotes to Scrape'
但是,直接在SelectorList
实例上使用.get()
可以避免IndexError
并且在找不到与选择匹配的任何元素时返回None
.
这里有一个教训:对于大多数抓取代码,您希望它能够对由于页面上未找到内容而导致的错误具有弹性,因此即使某些部分未能被抓取,您也至少可以获取一些数据.
除了getall()
和get()
方法,您还可以使用re()
方法使用正则表达式进行提取:
>>> response.css('title::text').re(r'Quotes.*')
['Quotes to Scrape']
>>> response.css('title::text').re(r'Q\w+')
['Quotes']
>>> response.css('title::text').re(r'(\w+) to (\w+)')
['Quotes', 'Scrape']
为了找到合适的CSS选择器,您可能会发现使用view(response)
从Web浏览器的外壳中打开响应页面很有用. 您可以使用浏览器的开发人员工具检查HTML并提供一个选择器(请参阅使用浏览器的开发人员工具进行抓取 ).
Selector Gadget还是一个不错的工具,可以快速为视觉选择的元素找到CSS选择器,该选择器可在许多浏览器中使用.
XPath: a brief intro¶
除了CSS之外,Scrapy选择器还支持使用XPath表达式:
>>> response.xpath('//title')
[<Selector xpath='//title' data='<title>Quotes to Scrape</title>'>]
>>> response.xpath('//title/text()').get()
'Quotes to Scrape'
XPath表达式非常强大,并且是Scrapy Selectors的基础. 实际上,CSS选择器是在后台转换为XPath的. 您可以看到,如果您仔细阅读外壳中选择器对象的文本表示形式.
尽管XPath表达式可能不如CSS选择器流行,但它提供了更多功能,因为除了浏览结构之外,它还可以查看内容. 使用XPath,您可以选择以下内容: 选择包含文本"下一页"的链接 . 这使XPath非常适合于抓取任务,并且即使您已经知道如何构造CSS选择器,我们也鼓励您学习XPath,这将使抓取更加容易.
我们不会在这里介绍XPath,但是您可以在此处阅读有关将XPath与Scrapy Selectors结合使用的更多信息. 要了解有关XPath的更多信息,我们建议本教程通过示例学习XPath ,并建议本教程学习"如何在XPath中思考" .
Extracting quotes and authors¶
现在您对选择和提取有所了解,让我们通过编写代码从网页中提取引号来完善蜘蛛程序.
http://quotes.toscrape.com中的每个引用都由如下所示的HTML元素表示:
<div class="quote">
<span class="text">“The world as we have created it is a process of our
thinking. It cannot be changed without changing our thinking.”</span>
<span>
by <small class="author">Albert Einstein</small>
<a href="/author/Albert-Einstein">(about)</a>
</span>
<div class="tags">
Tags:
<a class="tag" href="/tag/change/page/1/">change</a>
<a class="tag" href="/tag/deep-thoughts/page/1/">deep-thoughts</a>
<a class="tag" href="/tag/thinking/page/1/">thinking</a>
<a class="tag" href="/tag/world/page/1/">world</a>
</div>
</div>
让我们打开scrapy shell并进行一些尝试,以了解如何提取所需的数据:
$ scrapy shell 'http://quotes.toscrape.com'
我们获得带有HTML报价的选择器的列表,其中包括:
>>> response.css("div.quote")
上面的查询返回的每个选择器都允许我们在其子元素上运行进一步的查询. 让我们将第一个选择器分配给一个变量,以便我们可以在特定的引号上直接运行CSS选择器:
>>> quote = response.css("div.quote")[0]
Now, let’s extract text
, author
and the tags
from that quote
using the quote
object we just created:
>>> text = quote.css("span.text::text").get()
>>> text
'“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”'
>>> author = quote.css("small.author::text").get()
>>> author
'Albert Einstein'
鉴于标签是字符串列表,我们可以使用.getall()
方法来获取所有标签:
>>> tags = quote.css("div.tags a.tag::text").getall()
>>> tags
['change', 'deep-thoughts', 'thinking', 'world']
在弄清楚如何提取每一位之后,我们现在可以遍历所有引号元素并将它们放到Python字典中:
>>> for quote in response.css("div.quote"):
... text = quote.css("span.text::text").get()
... author = quote.css("small.author::text").get()
... tags = quote.css("div.tags a.tag::text").getall()
... print(dict(text=text, author=author, tags=tags))
{'tags': ['change', 'deep-thoughts', 'thinking', 'world'], 'author': 'Albert Einstein', 'text': '“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”'}
{'tags': ['abilities', 'choices'], 'author': 'J.K. Rowling', 'text': '“It is our choices, Harry, that show what we truly are, far more than our abilities.”'}
... a few more of these, omitted for brevity
>>>
Extracting data in our spider¶
让我们回到蜘蛛. 到目前为止,它没有特别提取任何数据,只是将整个HTML页面保存到本地文件中. 让我们将上面的提取逻辑集成到我们的Spider中.
Scrapy Spider通常会生成许多字典,其中包含从页面提取的数据. 为此,我们在回调中使用yield
Python关键字,如下所示:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
如果运行此蜘蛛,它将输出提取的数据和日志:
2016-09-19 18:57:19 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.toscrape.com/page/1/>
{'tags': ['life', 'love'], 'author': 'André Gide', 'text': '“It is better to be hated for what you are than to be loved for what you are not.”'}
2016-09-19 18:57:19 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.toscrape.com/page/1/>
{'tags': ['edison', 'failure', 'inspirational', 'paraphrased'], 'author': 'Thomas A. Edison', 'text': "“I have not failed. I've just found 10,000 ways that won't work.”"}
Storing the scraped data¶
存储已抓取数据的最简单方法是使用Feed输出 ,并使用以下命令:
scrapy crawl quotes -o quotes.json
这将生成一个quotes.json
文件,其中包含所有以JSON序列化的quotes.json
项.
由于历史原因,Scrapy会附加到给定文件,而不是覆盖其内容. 如果您两次运行此命令而没有在第二次之前删除该文件,那么最终将得到一个损坏的JSON文件.
您还可以使用其他格式,例如JSON Lines :
scrapy crawl quotes -o quotes.jl
JSON Lines格式很有用,因为它类似于流,您可以轻松地向其追加新记录. 当您运行两次时,它不会出现相同的JSON问题. 另外,由于每条记录都是单独的一行,因此您可以处理大文件而不必将所有内容都放入内存中,因此有类似JQ的工具可以在命令行中帮助您完成此操作.
在小型项目中(例如本教程中的项目),这应该足够了. 但是,如果要对已刮除的物料执行更复杂的操作,则可以编写" 物料管道" . 创建项目时,已在tutorial/pipelines.py
为您设置了Item Pipelines的占位符文件. 虽然如果您只想存储已刮除的项目,则无需实现任何项目管道.
Following links¶
比方说,您不仅要从http://quotes.toscrape.com的前两个页面中抓取内容,还需要网站中所有页面的引用.
Now that you know how to extract data from pages, let’s see how to follow links from them.
首先是将链接提取到我们要关注的页面. 检查我们的页面,我们可以看到指向下一页的链接,带有以下标记:
<ul class="pager">
<li class="next">
<a href="/page/2/">Next <span aria-hidden="true">→</span></a>
</li>
</ul>
我们可以尝试将其提取到shell中:
>>> response.css('li.next a').get()
'<a href="/page/2/">Next <span aria-hidden="true">→</span></a>'
这获得了anchor元素,但是我们需要属性href
. 为此,Scrapy支持CSS扩展,可让您选择属性内容,如下所示:
>>> response.css('li.next a::attr(href)').get()
'/page/2/'
还有一个attrib
属性可用(有关更多信息,请参见选择元素属性 ):
>>> response.css('li.next a').attrib['href']
'/page/2'
现在让我们看一下我们的Spider,将其修改为以递归方式链接到下一页的链接,并从中提取数据:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)
现在,在提取数据之后, parse()
方法将查找到下一页的链接,使用urljoin()
方法构建完整的绝对URL(因为链接可以是相对的),并产生对下一页的新请求,将其自身注册为回调,以处理下一页的数据提取并保持所有页面的爬网.
您在这里看到的是Scrapy的以下链接机制:当您在回调方法中产生请求时,Scrapy将安排该请求的发送并在该请求完成时注册要执行的回调方法.
使用此工具,您可以构建复杂的搜寻器,并根据定义的规则遵循链接,并根据其访问的页面提取不同类型的数据.
在我们的示例中,它创建了一个循环,将其链接到下一页的所有链接,直到找不到为止-方便您通过分页方式爬网博客,论坛和其他网站.
A shortcut for creating Requests¶
作为创建Request对象的快捷方式,您可以使用response.follow
:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
start_urls = [
'http://quotes.toscrape.com/page/1/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('span small::text').get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
yield response.follow(next_page, callback=self.parse)
与scrapy.Request不同, response.follow
直接支持相对URL-无需调用urljoin. 注意response.follow
仅返回一个Request实例; 您仍然必须产生此请求.
您也可以将选择器传递给response.follow
而不是字符串. 该选择器应提取必要的属性:
for href in response.css('li.next a::attr(href)'):
yield response.follow(href, callback=self.parse)
对于<a>
元素,有一个快捷方式: response.follow
自动使用其href属性. 因此,代码可以进一步缩短:
for a in response.css('li.next a'):
yield response.follow(a, callback=self.parse)
Note
response.follow(response.css('li.next a'))
无效,因为response.css
返回具有所有结果选择器的列表状对象,而不是单个选择器. 像上面示例中的for
循环,或response.follow(response.css('li.next a')[0])
都可以.
More examples and patterns¶
这是另一个说明回叫和后续链接的蜘蛛网,这次是用于抓取作者信息:
import scrapy
class AuthorSpider(scrapy.Spider):
name = 'author'
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
# follow links to author pages
for href in response.css('.author + a::attr(href)'):
yield response.follow(href, self.parse_author)
# follow pagination links
for href in response.css('li.next a::attr(href)'):
yield response.follow(href, self.parse)
def parse_author(self, response):
def extract_with_css(query):
return response.css(query).get(default='').strip()
yield {
'name': extract_with_css('h3.author-title::text'),
'birthdate': extract_with_css('.author-born-date::text'),
'bio': extract_with_css('.author-description::text'),
}
这个parse_author
将从首页开始,它将跟随指向作者页面的所有链接,并为每个作者页面调用parse_author
回调,以及如前所见的带有parse
回调的分页链接.
在这里,我们将回调作为位置参数传递给response.follow
,以使代码更短; 它也适用于scrapy.Request
.
parse_author
回调定义了一个辅助函数,用于从CSS查询中提取和清除数据,并生成包含作者数据的Python字典.
该蜘蛛演示的另一件有趣的事情是,即使同一位作者的引文很多,我们也不必担心多次访问同一作者页面. 默认情况下,Scrapy过滤掉对已访问URL的重复请求,避免了由于编程错误而导致服务器过多访问的问题. 可以通过设置DUPEFILTER_CLASS
进行配置.
希望到目前为止,您已经对如何在Scrapy中使用跟踪链接和回调的机制有了很好的了解.
作为另一个利用以下链接机制的蜘蛛示例,请查看CrawlSpider
类中的通用蜘蛛,该类实现了一个小的规则引擎,您可以使用该规则引擎在其上编写爬虫.
同样,一种常见的模式是使用一个技巧将更多数据传递给回调 ,从而使用来自多个页面的数据来构建项目.
Using spider arguments¶
您可以在运行蜘蛛时使用-a
选项为蜘蛛提供命令行参数:
scrapy crawl quotes -o quotes-humor.json -a tag=humor
这些参数将传递给Spider的__init__
方法,并默认成为蜘蛛属性.
在此示例中,可以通过self.tag
获得为tag
参数提供的值. 您可以使用它使您的Spider只获取带有特定标记的引号,并根据参数构建URL:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
url = 'http://quotes.toscrape.com/'
tag = getattr(self, 'tag', None)
if tag is not None:
url = url + 'tag/' + tag
yield scrapy.Request(url, self.parse)
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
}
next_page = response.css('li.next a::attr(href)').get()
if next_page is not None:
yield response.follow(next_page, self.parse)
如果将tag=humor
参数传递给该蜘蛛,您会注意到它只会访问来自humor
标签的URL,例如http://quotes.toscrape.com/tag/humor
.
您可以在此处了解更多有关处理蜘蛛参数的信息 .
Next steps¶
本教程仅介绍了Scrapy的基础知识,但这里没有提到很多其他功能. 检查还有什么? Scrapy中的"概览"一章,以快速概述最重要的主题.
您可以从" 基本概念 "部分继续,以进一步了解命令行工具,蜘蛛程序,选择器以及本教程未涵盖的其他内容,例如对抓取的数据进行建模. 如果您喜欢玩示例项目,请查看" 示例"部分.