Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath to href that contains certain keyword in link itself

What i need is to find all the links on the page that have some keyword inside the link itself. So , based on some stack topics i build my xpath as follows:

response.xpath('//a[contains(@href,'/Best-Sellers-Health-Personal-Care')]')

which should return a link like = "https://www.amazon.com/Best-Sellers-Health-Personal-Care-Tongue......"

But i get Invalid syntax error all the time. What am I mistaken? So what i do now is just add the if contains check when iterating through the list. But hoped there were more elegant and fast solution.

like image 700
Billy Jhon Avatar asked Feb 05 '23 00:02

Billy Jhon


1 Answers

This is because of inconsistent quotes usage.

Just replace

'//a[contains(@href,'/Best-Sellers-Health-Personal-Care')]'

with

'//a[contains(@href,"/Best-Sellers-Health-Personal-Care")]'
like image 96
Andersson Avatar answered Feb 08 '23 15:02

Andersson