【发布时间】:2015-02-25 11:29:12
【问题描述】:
我正在使用 scrapy 0.24 从网站上抓取数据。但是,我无法从我的回调方法parse_summary 发出任何请求。
class ExampleSpider(scrapy.Spider):
name = "tfrrs"
allowed_domains = ["example.org"]
start_urls = (
'http://www.example.org/results_search.html?page=0&sport=track&title=1&go=1',
)
def __init__(self, *args, **kwargs):
super(TfrrsSpider, self).__init__(*args, **kwargs)
self.start_urls = ['http://www.example.org/results_search.html?page=0&sport=track'&title=1&go=1',]
pass
# works without issue
def parse(self, response):
races = response.xpath("//table[@width='100%']").xpath(".//a[starts-with(@href, 'http://www.tfrrs.org/results/')]/@href").extract()
callback = self.parse_trackfieldsummary
for race in races:
yield scrapy.Request(race, callback=self.parse_summary)
pass
# works without issue
def parse_summary(self, response):
baseurl = 'http://www.example.org/results/'
results = response.xpath("//div[@class='data']").xpath('.//a[@style="margin-left: 20px;"]/@href').extract()
for result in results:
print(baseurl+result) # shows that url is correct everytime
yield scrapy.Request(baseurl+result, callback=self.parse_compiled)
# is never fired or shown in terminal
def parse_compiled(self, response):
print('test')
results = response.xpath("//table[@style='width: 935px;']")
print(results)
当我故意在parse_summary 中发出请求失败(由于域错误等)时,我能够在提示中看到错误,但是当我使用正确的 url 时,就好像我什至没有调用它。我还在parse 方法中测试了parse_summary 中请求的url,它们按预期工作。是什么导致它们没有在parse_summary 方法中被解雇但在parse method 中成功?提前感谢您的帮助。
编辑
对我的Spider 进行一些更改后,我仍然得到相同的结果。但是,如果我使用一个全新的项目,它会起作用。所以我猜这与我的项目设置有关。
这是我的项目设置(raceretrieval 是我的项目名称):
BOT_NAME = 'raceretrieval'
DOWNLOAD_DELAY= 1
CONCURRENT_REQUESTS = 100
SPIDER_MODULES = ['raceretrieval.spiders']
NEWSPIDER_MODULE = 'raceretrieval.spiders'
ITEM_PIPELINES = {
'raceretrieval.pipelines.RaceValidationPipeline':1,
'raceretrieval.pipelines.RaceDistanceValidationPipeline':2,
# 'raceretrieval.pipelines.RaceUploadPipeline':9999
}
如果我注释掉 both DOWNLOAD_DELAY= 1 和 CONCURRENT_REQUESTS = 100,蜘蛛会按预期工作。为什么会这样?我不明白他们会如何影响这一点。
【问题讨论】:
-
在 Reuques 中添加
dont_filter=True是否有效?顺便说一句,绝对网址:urlparse.urljoin(response.url, result)可能会好一点。 -
您分享的代码需要一些改进。拼写错误很少,您使用 2 个不同的网址(example.org tfrrs.org)并且缺少方法(parse_trackfieldsummary)。
标签: python python-2.7 callback web-scraping scrapy