如何在Scrapy 1.2.0中的Xpath表达式中传递字符串

时间:2016-10-13 16:20:32

标签: xpath scrapy

我无法在我的Scrapy代码中将Xpath Expression作为字符串变量传递。代码如下:

def start_requests(self):
    urls = [
    'http://www.example.com'
    ]
    for url in urls:
        yield scrapy.Request(url=url, callback=self.parse)

def parse(self, response):

strvar = "'//title'"
    print (strvar)
    print (response.xpath(strvar))
    print (response.xpath('//title'))

以上两个response.xpath(xpath表达式)查询以

的形式计算不同的xpath
Selector xpath="'//title'"  .... 
Selector xpath='//title'    ....

无法弄清楚我哪里出错了。

1 个答案:

答案 0 :(得分:1)

您不需要输入内部引号,替换:

strvar = "'//title'"

只是:

strvar = "//title"