未验证 提交 9d9a0ce8 编写于 作者: 飞龙 提交者: GitHub

Merge pull request #2 from jiez1812/master

Scrapy教程 翻译至row 157
......@@ -2,9 +2,9 @@
> 译者:[OSGeo 中国](https://www.osgeo.cn/)
在本教程中,我们假定scrapy已经安装在您的系统上。如果不是这样的话,看 [安装指南](install.html#intro-install) .
在本教程中,我们假定scrapy已经安装在您的系统上。如果还未安装,看 [安装指南](install.html#intro-install) .
我们要刮 [quotes.toscrape.com](http://quotes.toscrape.com/) 这是一个网站,列出了著名作家的名言
我们将回爬 [quotes.toscrape.com](http://quotes.toscrape.com/)的网站, 这是一家列出著名作家的名言的网站
本教程将指导您完成以下任务:
......@@ -14,7 +14,7 @@
4. 将spider改为递归跟踪链接
5. 使用 Spider 参数
Scrapy是用 Python 写的。如果你对这门语言不熟悉,你可能想从了解这门语言是什么开始,从 Scrapy 语言中得到最大的收获。
Scrapy是用 Python 写的。如果您对这门语言不熟悉,您可能想从了解这门语言是什么开始,从 Scrapy 语言中得到最大的收获。
如果您已经熟悉其他语言,并且希望快速学习Python,那么 [Python Tutorial](https://docs.python.org/3/tutorial) 是一种很好的资源。
......@@ -24,11 +24,11 @@ Scrapy是用 Python 写的。如果你对这门语言不熟悉,你可能想从
* [How To Think Like a Computer Scientist](http://openbookproject.net/thinkcs/python/english3e/)
* [Learn Python 3 The Hard Way](https://learnpythonthehardway.org/python3/)
也可以看看 [this list of Python resources for non-programmers](https://wiki.python.org/moin/BeginnersGuide/NonProgrammers) 以及 [suggested resources in the learnpython-subreddit](https://www.reddit.com/r/learnpython/wiki/index#wiki_new_to_python.3F) .
也可以看看 [this list of Python resources for non-programmers](https://wiki.python.org/moin/BeginnersGuide/NonProgrammers) 以及 [suggested resources in the learnpython-subreddit](https://www.reddit.com/r/learnpython/wiki/index#wiki_new_to_python.3F) .
## 创建项目
在开始抓取之前,你必须建立一个新的零碎项目。输入要在其中存储代码并运行的目录
在开始抓取之前,您必须建立一个新的Scrapy项目。首先,进入您想要储存代码的目录,并运行以下代码
```py
scrapy startproject tutorial
......@@ -59,9 +59,9 @@ tutorial/
## 我们的第一只 Spider
Spider 是你定义的类,Scrapy用来从一个网站(或一组网站)获取信息。它们必须是子类 `scrapy.Spider` 定义要发出的初始请求,可以选择如何跟踪页面中的链接,以及如何解析下载的页面内容以提取数据。
Spider 是您定义的类,Scrapy用来从一个网站(或一组网站)爬取信息。它们必须是 `scrapy.Spider` 子类以及定义要发出的初始请求,可以选择如何跟踪页面中的链接,以及如何解析下载的页面内容以提取数据。
这是我们第一只 Spider 的代码。将其保存在名为的文件中 `quotes_spider.py``tutorial/spiders` 项目中的目录
这是我们第一只 Spider 的代码。将其保存在 `tutorial/spiders` 目录下的 `quotes_spider.py` 文件里
```py
import scrapy
......@@ -86,13 +86,13 @@ class QuotesSpider(scrapy.Spider):
```
你所见,我们的 Spider 亚纲 [`scrapy.Spider`](../topics/spiders.html#scrapy.spiders.Spider "scrapy.spiders.Spider") 并定义了一些属性和方法
您所见,这是我们的 Spider [`scrapy.Spider`](../topics/spiders.html#scrapy.spiders.Spider "scrapy.spiders.Spider") 的子类。其属性和方法的定义如下
* [`name`](../topics/spiders.html#scrapy.spiders.Spider.name "scrapy.spiders.Spider.name")标识 Spider 。它在一个项目中必须是唯一的,也就是说,不能为不同的 Spider 设置相同的名称。
* [`name`](../topics/spiders.html#scrapy.spiders.Spider.name "scrapy.spiders.Spider.name")Spider的标识 。它在一个项目中必须是唯一的,也就是说,您不能为不同的 Spider 设置相同的名称。
* [`start_requests()`](../topics/spiders.html#scrapy.spiders.Spider.start_requests "scrapy.spiders.Spider.start_requests") :必须返回一个ITable of requests(您可以返回一个请求列表或编写一个生成器函数), Spider 将从中开始爬行。随后的请求将从这些初始请求中依次生成。
* [`start_requests()`](../topics/spiders.html#scrapy.spiders.Spider.start_requests "scrapy.spiders.Spider.start_requests") :必须返回可迭代的请求(您可以返回一个请求列表或编写一个生成器函数), Spider 将从中开始爬行。随后的请求将从这些初始请求中依次生成。
* [`parse()`](../topics/spiders.html#scrapy.spiders.Spider.parse "scrapy.spiders.Spider.parse") :将调用的方法,用于处理为每个请求下载的响应。响应参数是的实例 [`TextResponse`](../topics/request-response.html#scrapy.http.TextResponse "scrapy.http.TextResponse") 它保存页面内容,并有进一步有用的方法来处理它。
* [`parse()`](../topics/spiders.html#scrapy.spiders.Spider.parse "scrapy.spiders.Spider.parse") :将调用的方法,用于处理为每个请求下载的响应。响应参数是 [`TextResponse`](../topics/request-response.html#scrapy.http.TextResponse "scrapy.http.TextResponse") 的实例,它保存页面内容,并进一步处理它。
这个 [`parse()`](../topics/spiders.html#scrapy.spiders.Spider.parse "scrapy.spiders.Spider.parse") 方法通常解析响应,将抓取的数据提取为dict,并查找新的URL以跟踪和创建新的请求。( [`Request`](../topics/request-response.html#scrapy.http.Request "scrapy.http.Request") 从他们那里。
......@@ -105,7 +105,7 @@ scrapy crawl quotes
```
此命令运行名为的spider `quotes` 我们刚刚添加的,这将发送一些 `quotes.toscrape.com` 领域。您将得到类似于以下内容的输出:
此命令运行名为 `quotes` 的spider,这将发送一些请求至 `quotes.toscrape.com` 领域。您将得到类似于以下内容的输出:
```py
... (omitted for brevity)
......@@ -122,7 +122,7 @@ scrapy crawl quotes
```
现在,检查当前目录中的文件。您应该注意到已经创建了两个新文件: _quotes-1.html_ 和 引用-2.HTML, 将各个URL的内容作为 `parse` 方法指示
现在,检查当前目录中的文件。您应该注意有两个的新文件已经被创建: _quotes-1.html_ 和 _quotes-2.html_。如 ` parse ` 方法所示,每个文件都含有URL
注解
......@@ -130,7 +130,7 @@ scrapy crawl quotes
#### 引擎盖下面发生了什么?
Scrapy 时间表 [`scrapy.Request`](../topics/request-response.html#scrapy.http.Request "scrapy.http.Request") 返回的对象 `start_requests` Spider 的方法。在接收到每个响应时,它实例化 [`Response`](../topics/request-response.html#scrapy.http.Response "scrapy.http.Response") 对象并调用与请求关联的回调方法(在本例中,为 `parse` 方法)将响应作为参数传递。
Scrapy 安排被 Spider 里 `start_requests` 方法所返回的对象,也就是 [`scrapy.Request`](../topics/request-response.html#scrapy.http.Request "scrapy.http.Request") 。在接收到每个响应时,它实例化 [`Response`](../topics/request-response.html#scrapy.http.Response "scrapy.http.Response") 对象并调用与请求关联的回调方法(在本例中,为 `parse` 方法)将响应作为参数传递。
### 启动请求方法的快捷方式
......@@ -223,7 +223,7 @@ scrapy shell "http://quotes.toscrape.com/page/1/"
```
另一件事是呼叫的结果 `.getall()` 是一个列表:一个选择器可能返回多个结果,所以我们提取所有结果。当你知道你只想得到第一个结果时,就像在本例中一样,你可以这样做:
另一件事是呼叫的结果 `.getall()` 是一个列表:一个选择器可能返回多个结果,所以我们提取所有结果。当您知道您只想得到第一个结果时,就像在本例中一样,您可以这样做:
```py
>>> response.css('title::text').get()
......@@ -231,7 +231,7 @@ scrapy shell "http://quotes.toscrape.com/page/1/"
```
作为替代方案,可以写:
作为替代方案,可以写:
```py
>>> response.css('title::text')[0].get()
......@@ -421,7 +421,7 @@ scrapy crawl quotes -o quotes.jl
## 以下链接
比如说,不需要从http://quotes.toscrape.com的前两页抓取内容,而是需要从网站上所有页面的引用。
比如说,不需要从http://quotes.toscrape.com的前两页抓取内容,而是需要从网站上所有页面的引用。
既然您知道了如何从页面中提取数据,那么让我们看看如何从页面中跟踪链接。
......@@ -629,9 +629,9 @@ class QuotesSpider(scrapy.Spider):
```
如果通过 `tag=humor` 对于这个 Spider ,您会注意到它只访问来自 `humor` 标记,如 `http://quotes.toscrape.com/tag/humor` .
如果通过 `tag=humor` 对于这个 Spider ,您会注意到它只访问来自 `humor` 标记,如 `http://quotes.toscrape.com/tag/humor` .
可以 [learn more about handling spider arguments here](../topics/spiders.html#spiderargs) .
可以 [learn more about handling spider arguments here](../topics/spiders.html#spiderargs) .
## 下一步
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册