【发布时间】:2016-09-20 09:44:08
【问题描述】:
我正在尝试从此 Xpath 中提取文本:
//*/li[contains(., "Full Name")]/span/text()
来自此网页: http://votesmart.org/candidate/biography/56110/norma-smith#.V9SwdZMrKRs
我已经在 Google Chrome 的控制台(可以工作)中测试了它,就像 Xpath 的许多其他变体一样,但我无法让它与 Scrapy 一起使用。我的代码只返回“{}”。
这是我在代码中测试它的地方,用于上下文:
def parse_bio(self, response):
loader = response.meta['loader']
fullnameValue = response.xpath('//*/li[contains(., "Full Name")]/span/text()').extract()
loader.add_value('fullName', fullnameValue)
return loader.load_item()
问题不在于我的代码(我不认为),它适用于其他(非常广泛的)Xpath 选择器。但我不确定 Xpath 有什么问题。我禁用了 JavaScript,如果这有什么不同的话。 任何帮助都会很棒!
编辑:为了更清楚,这里是其余的代码:
from scrapy import Spider, Request, Selector
from votesmart.items import LegislatorsItems, TheLoader
class VSSpider(Spider):
name = "vs"
allowed_domains = ["votesmart.org"]
start_urls = ["https://votesmart.org/officials/WA/L/washington-state-legislative"]
def parse(self, response):
for href in response.xpath('//h5/a/@href').extract():
person_url = response.urljoin(href)
yield Request(person_url, callback=self.candidatesPoliticalSummary)
def candidatesPoliticalSummary(self, response):
item = LegislatorsItems()
l = TheLoader(item=LegislatorsItems(), response=response)
...
#populating items with item loader. works fine
# create right bio url and pass item loader to it
bio_url = response.url.replace('votesmart.org/candidate/',
'votesmart.org/candidate/biography/')
return Request(bio_url, callback=self.parse_bio, meta={'loader': l})
def parse_bio(self, response):
loader = response.meta['loader']
print response.request.url
loader.add_xpath('fullName', '//*/li[contains(., "Full Name")]/span/text()')
return loader.load_item()
【问题讨论】: