+1 vote
in Programming Languages by (51.1k points)
I want to find all "a" tags on a web page that contains some particular string. How can I find it using BeautifulSoup?

1 Answer

+1 vote
by (157k points)
selected by
Best answer

You can use the find_all() function to find all "a" tags on a webpage. To find a particular string inside the "a" tag, you need to use the argument "string."

Here is an example. I am searching for "tom" inside the "a" tag.

from bs4 import BeautifulSoup
import urllib.request as ur

url = "url_of_the_webpage"
req = ur.Request(url, None, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' })
rs = ur.urlopen(req)

soup = BeautifulSoup(rs, 'html.parser')
for sp in soup.find_all("a", string="tom"):