# Scrapy at a glance
> 譯者:[OSGeo 中國](https://www.osgeo.cn/)
Scrapy是一個應用程序框架,用于對網站進行爬行和提取結構化數據,這些結構化數據可用于各種有用的應用程序,如數據挖掘、信息處理或歷史存檔。
盡管Scrapy最初是為 [web scraping](https://en.wikipedia.org/wiki/Web_scraping) 它還可以用于使用API提取數據(例如 [Amazon Associates Web Services](https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html) )或者作為一個通用的網絡爬蟲。
## 瀏覽示例 Spider
為了向您展示Scrapy給桌子帶來了什么,我們將用最簡單的方法來運行一個Scrapy Spider 的例子。
以下是一個 Spider 代碼,它從網站http://quotes.toscrape.com上抓取著名的引語,按照以下頁碼:
```py
import scrapy
class QuotesSpider(scrapy.Spider):
name = 'quotes'
start_urls = [
'http://quotes.toscrape.com/tag/humor/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield {
'text': quote.css('span.text::text').get(),
'author': quote.xpath('span/small/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)
```
把它放在一個文本文件中,命名為 `quotes_spider.py` 然后用 [`runspider`](../topics/commands.html#std:command-runspider) 命令:
```py
scrapy runspider quotes_spider.py -o quotes.json
```
完成后,您將 `quotes.json` 以JSON格式提交一個引號列表,其中包含文本和作者,如下所示(此處重新格式化以提高可讀性):
```py
[{
"author": "Jane Austen",
"text": "\u201cThe person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.\u201d"
},
{
"author": "Groucho Marx",
"text": "\u201cOutside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.\u201d"
},
{
"author": "Steve Martin",
"text": "\u201cA day without sunshine is like, you know, night.\u201d"
},
...]
```
### 剛剛發生了什么?
當你運行命令時 `scrapy runspider quotes_spider.py` 斯克里奇在里面尋找 Spider 的定義,然后用它的爬行引擎運行。
通過向中定義的URL發出請求啟動的爬網 `start_urls` 屬性(在本例中,只有引號的URL _humor_ 并調用默認回調方法 `parse` ,將響應對象作為參數傳遞。在 `parse` 回調,我們使用CSS選擇器循環引用元素,生成一個包含提取的引號文本和作者的python dict,查找到下一頁的鏈接,并使用它調度另一個請求。 `parse` 方法作為回調。
在這里,您注意到Scrapy的一個主要優點:請求是 [scheduled and processed asynchronously](../topics/architecture.html#topics-architecture) . 這意味著Scrapy不需要等待請求完成和處理,它可以同時發送另一個請求或做其他事情。這也意味著,即使某些請求失敗或在處理過程中發生錯誤,其他請求也可以繼續進行。
雖然這使您能夠非常快速地進行爬行(同時以容錯的方式發送多個并發請求),但Scrapy還使您能夠控制爬行的禮貌性。 [a few settings](../topics/settings.html#topics-settings-ref) . 您可以在每個請求之間設置下載延遲、限制每個域或每個IP的并發請求量,甚至 [using an auto-throttling extension](../topics/autothrottle.html#topics-autothrottle) 它試圖自動解決這些問題。
注解
這是使用 [feed exports](../topics/feed-exports.html#topics-feed-exports) 要生成JSON文件,您可以輕松地更改導出格式(例如XML或CSV)或存儲后端(FTP或 [Amazon S3](https://aws.amazon.com/s3/) 例如)。你也可以寫一個 [item pipeline](../topics/item-pipeline.html#topics-item-pipeline) 將項目存儲在數據庫中。
## 還有什么?
你已經看到了如何使用Scrapy從網站中提取和存儲項目,但這只是表面現象。Scrapy提供了許多強大的功能,使抓取變得簡單和高效,例如:
* 內置支持 [selecting and extracting](../topics/selectors.html#topics-selectors) 使用擴展的CSS選擇器和XPath表達式從HTML/XML源中獲取數據,并使用正則表達式提取助手方法。
* 安 [interactive shell console](../topics/shell.html#topics-shell) (ipython-aware)用于嘗試使用css和xpath表達式來獲取數據,在編寫或調試spider時非常有用。
* 內置支持 [generating feed exports](../topics/feed-exports.html#topics-feed-exports) 以多種格式(json、csv、xml)存儲在多個后端(ftp、s3、本地文件系統)
* 強大的編碼支持和自動檢測,用于處理外部、非標準和中斷的編碼聲明。
* [Strong extensibility support](../index.html#extending-scrapy) ,允許您使用 [signals](../topics/signals.html#topics-signals) 以及定義良好的API(中間件, [extensions](../topics/extensions.html#topics-extensions) 和 [pipelines](../topics/item-pipeline.html#topics-item-pipeline) )
* 廣泛的內置擴展和用于處理的中間產品:
* cookie和會話處理
* HTTP功能,如壓縮、身份驗證、緩存
* 用戶代理欺騙
* robots.txt
* 爬行深度限制
* 更多
* A [Telnet console](../topics/telnetconsole.html#topics-telnetconsole) 用于掛接到運行在Scrapy進程中的Python控制臺,以便內省和調試爬蟲程序
* 還有其他的好東西,比如可重復使用的 Spider [Sitemaps](https://www.sitemaps.org/index.html) 和XML/CSV源,這是 [automatically downloading images](../topics/media-pipeline.html#topics-media-pipeline) (或任何其他媒體)與抓取的項目、緩存DNS解析程序等相關!
## 下一步是什么?
接下來的步驟是 [install Scrapy](install.html#intro-install) , [follow through the tutorial](tutorial.html#intro-tutorial) 學習如何創建一個完整的 Scrapy 項目和 [join the community](https://scrapy.org/community/) . 感謝您的關注!
- 簡介
- 第一步
- Scrapy at a glance
- 安裝指南
- Scrapy 教程
- 實例
- 基本概念
- 命令行工具
- Spider
- 選擇器
- 項目
- 項目加載器
- Scrapy shell
- 項目管道
- Feed 導出
- 請求和響應
- 鏈接提取器
- 設置
- 例外情況
- 內置服務
- Logging
- 統計數據集合
- 發送電子郵件
- 遠程登錄控制臺
- Web服務
- 解決具體問題
- 常見問題
- 調試spiders
- Spider 合約
- 常用做法
- 通用爬蟲
- 使用瀏覽器的開發人員工具進行抓取
- 調試內存泄漏
- 下載和處理文件和圖像
- 部署 Spider
- AutoThrottle 擴展
- Benchmarking
- 作業:暫停和恢復爬行
- 延伸 Scrapy
- 體系結構概述
- 下載器中間件
- Spider 中間件
- 擴展
- 核心API
- 信號
- 條目導出器
- 其余所有
- 發行說明
- 為 Scrapy 貢獻
- 版本控制和API穩定性