gpt4 book ai didi

python - scrapy 无法进行 Request() 回调

转载 作者:行者123 更新时间:2023-11-28 17:47:47 28 4
gpt4 key购买 nike

我正在尝试用 Scrapy 制作递归解析脚本,但是 Request() 函数没有调用回调函数 suppose_to_parse(),也没有回调值中提供的任何函数.我尝试了不同的变体,但没有一个起作用。去哪里挖?

from scrapy.http import Request
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector



class joomler(BaseSpider):
name = "scrapy"
allowed_domains = ["scrapy.org"]
start_urls = ["http://blog.scrapy.org/"]


def parse(self, response):
print "Working... "+response.url
hxs = HtmlXPathSelector(response)
for link in hxs.select('//a/@href').extract():
if not link.startswith('http://') and not link.startswith('#'):
url=""
url=(self.start_urls[0]+link).replace('//','/')
print url
yield Request(url, callback=self.suppose_to_parse)


def suppose_to_parse(self, response):
print "asdasd"
print response.url

最佳答案

将 yield 移到 if 语句之外:

for link in hxs.select('//a/@href').extract():
url = link
if not link.startswith('http://') and not link.startswith('#'):
url = (self.start_urls[0] + link).replace('//','/')

print url
yield Request(url, callback=self.suppose_to_parse)

关于python - scrapy 无法进行 Request() 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15578918/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com