Scrapy之Request函数回调未执行 Filtered offsite request to 'XXX'
在爬取免费代理ip室发现,第一页爬取之后后续的多页爬取没有被解析。在查看调试信息时发现显示 Filtered offsite request to 'www.kuaidaili.com',经网上查找先关问题解决方案,发现为域名爬取限制,即allowed_domains = ['www.kuaidaili.com/']字段。
解决方法:1.在allowed_domains中加入 url
2.直接将allowed_domains = [],即不做限制
3.将scrapy.Request(url=url, callback=self.parse,dont_filter=True),中的dont_filter设置为True。
import scrapy from freeIP_spider.items import FreeipSpiderItem class FreeipSpider(scrapy.Spider): name = 'freeip' #allowed_domains = ['www.kuaidaili.com/'] allowed_domains =[] start_urls = ['http://www.kuaidaili.com/free/inha/1//'] # 爬取多页 url = 'http://www.kuaidaili.com/free/inha/{}/' page = 1 def parse(self, response): #实例化数据结构 item= FreeipSpiderItem() tr= response.xpath('//table//tbody/tr') #print('=====================',len(tr)) for td in tr: item['ip']= td.xpath('./td[1]/text()').extract_first() item['port']= td.xpath('./td[2]/text()').extract_first() item['ip_type']= td.xpath('./td[4]/text()').extract_first() # print('=====================') # print(item) # print('=====================') yield item # 爬取多页 if self.page < 5: self.page+=1 url = self.url.format(self.page) #yield scrapy.Request(url=url, callback=self.parse,dont_filter=True) yield scrapy.Request(url=url, callback=self.parse)
浙公网安备 33010602011771号