Scrapy一覽https://doc.scrapy.org/en/latest/intro/overview.html
Scrapy是一個應(yīng)用程序框架咙轩,為各種各樣的應(yīng)用程序爬取網(wǎng)站提取結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)挖掘流酬,信息處理或者歷史檔案初坠。
Scrapy不止可以做網(wǎng)站的數(shù)據(jù)提取炸站,也可以用于APIs(如 Amazon Associates Web Services)的數(shù)據(jù)提取或者作為專用的web蜘蛛闹伪。
運(yùn)行一個簡單的蜘蛛
這是從 http://quotes.toscrape.com 網(wǎng)站獲取名言的蜘蛛代碼片段:
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').extract_first(),
'author': quote.xpath('span/small/text()').extract_first(),
}
next_page = response.css('li.next a::attr("href")').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)```
把代碼存放在文件中,命名為`quotes_spider.py`梁钾,使用`runspider`命令運(yùn)行蜘蛛绳泉。
`scrapy runspider quotes_spider.py -o quotes.json`
運(yùn)行結(jié)束時你會有個`quotes.json`文件列出所有的JSON格式名言逊抡,包括文本和作者姆泻,類似這樣(這里為了閱讀重新格式化了):
[{
"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"
},
...]
## 發(fā)生了什么
當(dāng)你運(yùn)行命令`scrapy runspider quotes_spider.py`時零酪,Scrapy找到代碼中蜘蛛的定義,然后在crawler引擎中運(yùn)行它拇勃。
蜘蛛從`start_urls`屬性中給定的URLS開始請求(此例只有quotes的humor目錄網(wǎng)址)四苇,然后調(diào)用默認(rèn)的回調(diào)函數(shù)`parse`,把請求結(jié)果作為參數(shù)方咆。在`parse`回調(diào)中月腋,我們使用CSS選擇器循環(huán)quote元素,生成一個含有quote文本和作者的python字典瓣赂,查找下一頁的鏈接地址計劃用另一個請求使用相同的`parse`方法回調(diào)榆骚。
此處你注意到Scrapy的主要優(yōu)點(diǎn):請求的計劃和處理都是異步的。這意味著Scrapy不需要等待一個請求的結(jié)束然后處理煌集,它可以發(fā)送另一個請求或者同時做其他的事情妓肢。這意味著即使有些請求失敗或者出錯了,其他的請求也會繼續(xù)運(yùn)行苫纤。
這可以使你快速爬去數(shù)據(jù)(同時發(fā)送多個請求)Scrapy通過[一些小的設(shè)置](https://doc.scrapy.org/en/latest/topics/settings.html#topics-settings-ref)可以使你的爬蟲更加禮貌碉钠。你可以設(shè)置每次請求之間的延遲,限制同時請求每個域名或ip的個數(shù)卷拘,或者直接使用 [using an auto-throttling extension](https://doc.scrapy.org/en/latest/topics/autothrottle.html#topics-autothrottle) 自動實(shí)現(xiàn)喊废。
#### 提示
此處使用JSON文件導(dǎo)出結(jié)果你也可以使用XML或CSV格式,或者使用pipline把item存到數(shù)據(jù)庫中栗弟。