[英]scrapy unable to make Request() callback
我試圖用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
我不是專家,但我嘗試了你的代碼,我認為問題不在請求上,生成的url似乎被打破了,如果你將一些url添加到列表並迭代它們並產生帶回調的Request ,它工作正常。
將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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.