Xpath选择器不在Scrapy中工作

时间:2016-09-12 00:27:17

标签: xpath scrapy

我试图从这个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()

2 个答案:

答案 0 :(得分:0)

表达式在shell中完美地适用于我:

$ scrapy shell "http://votesmart.org/candidate/biography/56110/norma-smith#.V9SwdZMrKRs"
In [1]: response.xpath('//*/li[contains(., "Full Name")]/span/text()').extract()
Out[1]: [u'Norma Smith']

请尝试使用add_xpath()方法:

loader.add_xpath('fullName', '//*/li[contains(., "Full Name")]/span/text()')

答案 1 :(得分:0)

我发现了我的问题!网站上的许多页面都是登录保护的,我无法从首先无法访问的页面中搜集。 Scrapy的表单请求起到了作用。感谢所有帮助(特别是使用view(response)的建议,这非常有用)。

相关问题