For instance if I am searching by an element's attribute like id:
soup.findAll('span',{'id':re.compile("^score_")})
I get back a list of the whole span element that matches (which I like).
But if I try to search by the innerText of the html element like this:
soup.findAll('a',text = re.compile("discuss|comment"))
I get back only the innerText part of element back that matches instead of the whole element with tags and attributes like I would above.
Is this possible to do with out finding the match and then getting it's parent?
Thanks.
You don't get back the text. You get a NavigableString
with the text. That object has methods to go to the parent, etc.
from BeautifulSoup import BeautifulSoup
import re
soup = BeautifulSoup('<html><p>foo</p></html>')
r = soup.findAll('p', text=re.compile('foo'))
print r[0].parent
prints
<p>foo</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With