我无法在我的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表达式)查询以
的形式计算不同的xpathSelector xpath="'//title'" ....
Selector xpath='//title' ....
无法弄清楚我哪里出错了。
答案 0 :(得分:1)
您不需要输入内部引号,替换:
strvar = "'//title'"
只是:
strvar = "//title"