Scrapy - 理解 CrawlSpider 和 LinkExtractor
Scrapy - Understanding CrawlSpider and LinkExtractor
所以我尝试使用 CrawlSpider 并理解 Scrapy Docs 中的以下示例:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class MySpider(CrawlSpider):
name = 'example.com'
allowed_domains = ['example.com']
start_urls = ['http://www.example.com']
rules = (
# Extract links matching 'category.php' (but not matching 'subsection.php')
# and follow links from them (since no callback means follow=True by default).
Rule(LinkExtractor(allow=('category\.php', ), deny=('subsection\.php', ))),
# Extract links matching 'item.php' and parse them with the spider's method parse_item
Rule(LinkExtractor(allow=('item\.php', )), callback='parse_item'),
)
def parse_item(self, response):
self.logger.info('Hi, this is an item page! %s', response.url)
item = scrapy.Item()
item['id'] = response.xpath('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
item['name'] = response.xpath('//td[@id="item_name"]/text()').extract()
item['description'] = response.xpath('//td[@id="item_description"]/text()').extract()
return item
当时给出的描述是:
This spider would start crawling example.com’s home page, collecting category links, and item links, parsing the latter with the parse_item method. For each item response, some data will be extracted from the HTML using XPath, and an Item will be filled with it.
我了解到,对于第二条规则,它从 item.php
中提取链接,然后使用 parse_item
方法提取信息。但是,第一条规则的目的到底是什么?它只是说它 "collects" 链接。这是什么意思,如果他们不从中提取任何数据,为什么有用?
CrawlSpider 在爬取论坛搜索帖子时非常有用,或者在搜索产品页面时对在线商店进行分类。
想法是 "somehow" 您必须进入每个类别,搜索与您要提取的 product/item 信息相对应的 link。这些产品 link 是在该示例的第二条规则中指定的产品(它表示 url 中具有 item.php
的产品)。
现在蜘蛛应该如何继续访问 links 直到找到包含 item.php
的那些?这是第一条规则。它说要访问每个包含 category.php
但不包含 subsection.php
的 Link,这意味着它不会完全从那些 link 中提取任何 "item",但它定义了蜘蛛寻找真实物品的路径。
这就是为什么您看到它在规则中不包含 callback
方法的原因,因为它不会 return 那个 link 响应供您处理,因为它会直接关注
所以我尝试使用 CrawlSpider 并理解 Scrapy Docs 中的以下示例:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class MySpider(CrawlSpider):
name = 'example.com'
allowed_domains = ['example.com']
start_urls = ['http://www.example.com']
rules = (
# Extract links matching 'category.php' (but not matching 'subsection.php')
# and follow links from them (since no callback means follow=True by default).
Rule(LinkExtractor(allow=('category\.php', ), deny=('subsection\.php', ))),
# Extract links matching 'item.php' and parse them with the spider's method parse_item
Rule(LinkExtractor(allow=('item\.php', )), callback='parse_item'),
)
def parse_item(self, response):
self.logger.info('Hi, this is an item page! %s', response.url)
item = scrapy.Item()
item['id'] = response.xpath('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
item['name'] = response.xpath('//td[@id="item_name"]/text()').extract()
item['description'] = response.xpath('//td[@id="item_description"]/text()').extract()
return item
当时给出的描述是:
This spider would start crawling example.com’s home page, collecting category links, and item links, parsing the latter with the parse_item method. For each item response, some data will be extracted from the HTML using XPath, and an Item will be filled with it.
我了解到,对于第二条规则,它从 item.php
中提取链接,然后使用 parse_item
方法提取信息。但是,第一条规则的目的到底是什么?它只是说它 "collects" 链接。这是什么意思,如果他们不从中提取任何数据,为什么有用?
CrawlSpider 在爬取论坛搜索帖子时非常有用,或者在搜索产品页面时对在线商店进行分类。
想法是 "somehow" 您必须进入每个类别,搜索与您要提取的 product/item 信息相对应的 link。这些产品 link 是在该示例的第二条规则中指定的产品(它表示 url 中具有 item.php
的产品)。
现在蜘蛛应该如何继续访问 links 直到找到包含 item.php
的那些?这是第一条规则。它说要访问每个包含 category.php
但不包含 subsection.php
的 Link,这意味着它不会完全从那些 link 中提取任何 "item",但它定义了蜘蛛寻找真实物品的路径。
这就是为什么您看到它在规则中不包含 callback
方法的原因,因为它不会 return 那个 link 响应供您处理,因为它会直接关注