Search by tag contents with BeautifulSoup
I would like to search for a particular tag by it's text contents. For example:
<a href="http://goingher开发者_运维知识库e.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?
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.
精彩评论