Spider爬取过程
以初始的URL初始化Request,并设置回调函数。当该request下载完毕并返回时,将生成response,并作为参数传递给该回调函数。
spider中初始的request是通过调用start_requests()来获取的。start_request()读取start_urls中的URL,并以parse为回调函数生成Request。
在回调函数内分析返回的(网页)内容,返回 Item 对象或者 Request 或者一个包括二者的可迭代容器。返回的Request对象之后会经过Scrapy处理,下载相应的内容并调用设置的callback函数(函数可相同)。
在回调函数内,您可以使用选择器(Selector、BeautifulSoup、lxml等)来分析网页内容,并根据分析的数据生成item。
最后,由spider返回的item将被存到数据库(由某些Item Pipeline处理)或使用 Feed exports存入到文件中。
Spider样例
代码如下 | 复制代码 |
import scrapy from myproject.items import MyItem class MySpider(scrapy.Spider): def parse(self, response): for url in response.xpath('//a/@href').extract(): |
CrawlSpider样例
代码如下 | 复制代码 |
import scrapy from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors import LinkExtractor class MySpider(CrawlSpider): name = 'example.com' allowed_domains = ['example.com'] start_urls = ['http://www.example.com'] # 在如下的规则中,第一条表示匹配category.php但是不匹配subsection.php(没有callback意味着follow为True表示跟进链接) # 在如下的规则中,第二条表示表示匹配item.php,并使用spider的parse_item方法进行分析。 rules = ( Rule(LinkExtractor(allow=('category\.php', ), deny=('subsection\.php', ))), Rule(LinkExtractor(allow=('item\.php', )), callback='parse_item'), ) def parse_item(self, response): self.log('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 |