この資料の冒頭から、私はScrapy爬虫類テンプレートを3件の記事を使用して説明します。Scrapy爬虫類テンプレートは、4つのテンプレートが含まれています。
- 基本:最も基本的なテンプレートは、ここでは説明しません。
- CrawlSpider
- XMLFeedSpider
- CSVFEEDSpider
この記事で私はCrawlSpiderテンプレートに説明します。
ゼロ、説明
CrawlSpiderは、一般的にカスタムルールによってリンクをたどるために、スパイダーを使用しています。サイトのほとんどのために我々は、タスクがルールを修正することによって達成することができますクロール。CrawlSpiderの共通属性は、ルールの1以上である*、ルールオブジェクトのタプルの形態を示します。各ルールオブジェクトは、標的部位をクロールの動作を定義します。
ヒント:複数のルールオブジェクトを持っている場合は、同じリンク、ルールオブジェクトの力への最初のエントリだけを打ちました。
私たちは、役割の構文を見て:
Rule(link_extractor [,callback = None] [,cb_kwargs = None] [,follow = None] [,process_links = None] [,process_request = None])
パラメータ解像度:
- link_extractor:リンクExtratorオブジェクトは正規表現です。抽出は、Webページからのリンクをたどるし続けての主な要素が定義されています。
- コールバック:コールバック関数で、それはコールバック関数の文字列名を指定できます。レスポンスは、パラメータとして受け取った、または項目リストのRequestオブジェクトを返します。
- cb_kwargs:タイプオブジェクト辞書、コールバック関数に渡されたパラメータ。
- 従ってください:このルールの応答に従ってlink_extractorからリンクを抽出するかどうか。
- process_links:コールバック関数は、コールバック関数の文字列名を指定できます。この関数は、リンクされたリストになってlink_extractorから呼び出されます。この方法は、主に、フィルタリングのために使用されます。
- process一:コールバック関数は、コールバック関数の文字列名を指定できます。リクエストをフィルタリングするために使用、ルールがそれぞれの要求を抽出するために、関数と呼ばれます。
I.ケース
私たちのクロールの有名な有名人のサイトで、私たちが行う必要がある。この場合は、著者のリンク、私たちのクロールの最終的な詳細の著者によるプレゼンテーションのページを入力し、有名な、著者名やラベルの内容を抽出します。
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class Quotes(CrawlSpider):
name = "quotes"
allow_domain = ['quotes.toscrape.com']
start_urls = ['http://quotes.toscrape.com']
rules = (
Rule(LinkExtractor(allow='/page/\d+'), callback='parse_quotes', follow=True),
Rule(LinkExtractor(allow='/author/\w+_'), callback='parse_author')
)
def parse_quotes(self, response):
for quote in response.css('.quote'):
yield {
'content': quote.css('.text::text').extract_first(),
'author': quote.css('.author::text').extract_first(),
'tags': quote.css('.tag::text').extract()
}
def parse_author(self, response):
name = response.css('.author-title::text').extract_first()
author_born_date = response.css('.author_born_date::text').extract_first()
author_description = response.css('.author_description::text').extract_first()
return ({
'name': name,
'author_born_date': author_born_date,
'author_description': author_description
})
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class Quotes(CrawlSpider):
name = "quotes"
allow_domain = ['quotes.toscrape.com']
start_urls = ['http://quotes.toscrape.com']
rules = (
Rule(LinkExtractor(allow='/page/\d+'), callback='parse_quotes', follow=True),
Rule(LinkExtractor(allow='/author/\w+_'), callback='parse_author')
)
def parse_quotes(self, response):
for quote in response.css('.quote'):
yield {
'content': quote.css('.text::text').extract_first(),
'author': quote.css('.author::text').extract_first(),
'tags': quote.css('.tag::text').extract()
}
def parse_author(self, response):
name = response.css('.author-title::text').extract_first()
author_born_date = response.css('.author_born_date::text').extract_first()
author_description = response.css('.author_description::text').extract_first()
return ({
'name': name,
'author_born_date': author_born_date,
'author_description': author_description
})
上記のコードRule(LinkExtractor(allow='/page/\d+'), callback='parse_quotes', follow=True),
スニペットは、彼らが満たしていることで有名な有名人のすべてのページをクロールするためのルールを定義する/page/\d+
リンクのすべてを好きなことわざページと考え、その後、我々は、関連するデータを抽出するためにparse_quotesメソッドを呼び出しています。ではRule(LinkExtractor(allow='/author/\w+_'), callback='parse_author')
、我々は、彼らが満たしていることを情報のページにクロールルール定義されたスニペット/author/\w+_
のすべてのリンクを著者情報ページと考え、その後、我々は、関連するデータを抽出するためにparse_authorメソッドを呼び出しています。
)代码段中我们定义了爬取作者信息页的规则,即只要符合
/著者/ \ wは+ _```すべてのリンクの著者情報ページとみなされ、その後、我々は、関連するデータを抽出するためにparse_authorメソッドを呼び出します。