Issue
I have this link:
<a href="/location/santa-clara/3fce50c4f3f9793d2f503fc145585090">Santa Clara, California</a>
How can I use BeautifulSoup to find specifically this link that includes the word location "location"
?
Solution
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)
Answered By - alecxe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.