这是一个简单的scrapy蜘蛛
import scrapy
class ExampleSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["https://www.dmoz.org"]
start_urls = ('https://www.dmoz.org/')
def parse(self,response):
yield scrapy.Request(self.start_urls[0],callback=self.parse2)
def parse2(self, response):
print(response.url)
Run Code Online (Sandbox Code Playgroud)
当您运行该程序时,parse2方法不起作用,它不会打印response.url.然后我在下面的帖子中找到了解决方案.
为什么我的第二个请求没有被我的scrapy蜘蛛的parse方法调用
它只是我需要在请求方法中添加dont_filter = True作为参数以使parse2函数工作.
yield scrapy.Request(self.start_urls[0],callback=self.parse2,dont_filter=True)
Run Code Online (Sandbox Code Playgroud)
但是在scrapy文档和许多youtube教程中给出的示例中,他们从未在scrapy.Request方法中使用dont_filter = True参数,但仍然可以使用其第二个解析函数.
看看这个
def parse_page1(self, response):
return scrapy.Request("http://www.example.com/some_page.html",
callback=self.parse_page2)
def parse_page2(self, response):
# this would log http://www.example.com/some_page.html
self.logger.info("Visited %s", response.url)
Run Code Online (Sandbox Code Playgroud)
除非添加dont_filter = True,为什么我的蜘蛛不能工作?我究竟做错了什么 ?在我的第一个例子中,我的蜘蛛过滤了哪些重复的链接?
PS我可以在上面发布的QA帖子中解决这个问题,但我不能发表评论,除非我有50个声望(可怜的我!!)