Find a link that contains a specific word using BeautifulSoup

You can do it with a simple “contains” CSS selector:

soup.select("a[href*=location]")

Or, if only one link needs to be matched, use select_one():

soup.select_one("a[href*=location]")

And, of course, there are many other ways – for instance, you can use find_all() providing the href argument which can have a regular expression value or a function:

import re

soup.find_all("a", href=re.compile("location"))
soup.find_all("a", href=lambda href: href and "location" in href)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)