Using find_parent() function. This does not find an <a> tag with the expression in it but find the regex and then try to check if it has a parent tag named "a" (at any superior level).
The good part is that find_parent() return an bs4.element.Tag instead of a bs4.element.NavigableString (returned by find_all + regular expression)
Xavier Combelle
unread,
Sep 14, 2012, 1:34:54 PM9/14/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to beauti...@googlegroups.com
you should not use
prettify
from bs4 import BeautifulSoup import re
html =
BeautifulSoup(open("index.html"), "lxml")
for parent in
html.find_all("font", text="U.R.P.")[0].parents: if
parent.name == "a": print
'found!' break