user5621062
user5621062

Reputation:

How to pass string in Xpath expression in Scrapy 1.2.0

I am not able to pass Xpath Expression as a string variable in my Scrapy code. Code below:

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'))

The above two response.xpath(xpath expression) queries evaluates to different xpaths as

Selector xpath="'//title'"  .... 
Selector xpath='//title'    ....

Can't figure out where am I going wrong.

Upvotes: 0

Views: 117

Answers (1)

alecxe
alecxe

Reputation: 473893

You don't need to put the inner quotes, replace:

strvar = "'//title'"

with just:

strvar = "//title"

Upvotes: 1

Related Questions