Using BeautifulSoup to find a HTML tag that contains certain text
from BeautifulSoup import BeautifulSoup import re html_text = “”” <h2>this is cool #12345678901</h2> <h2>this is nothing</h2> <h1>foo #126666678901</h1> <h2>this is interesting #126666678901</h2> <h2>this is blah #124445678901</h2> “”” soup = BeautifulSoup(html_text) for elem in soup(text=re.compile(r’ #\S{11}’)): print elem.parent Prints: <h2>this is cool #12345678901</h2> <h2>this is interesting #126666678901</h2> <h2>this is blah #124445678901</h2>