JBJ
JBJ

Reputation: 1109

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.

Upvotes: 4

Views: 9548

Answers (1)

Andersson
Andersson

Reputation: 52665

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

Upvotes: 10

Related Questions