2

I would like to search for a particular tag by it's text contents. For example:

<a href="http://goinghere.com">Lets go somewhere</a>

I want to find the above by searching for the text 'Lets go somewhere'. I am currently doing it using re. Can it be done in BeautifulSoup or is it better to use re in this case?

3eee3
  • 155
  • 1
  • 2
  • 7

1 Answers1

2
s = BeautifulSoup(...)
s.find(text='Lets go somewhere')

You can also use regex.

Using BeautifulSoup to find a HTML tag that contains certain text

Edit: While the find method prints a string if you use it on the command line, that's actually just a representation of the object it returns; you can access the parent attribute on it to access its BeautifulSoup tag object.

Community
  • 1
  • 1
kungphu
  • 4,592
  • 3
  • 28
  • 37