python - find_parent() method implementation - BeautifulSoup -
find_parent()
method different find_parents()
method because returns first immediate parent, unlike find_parents()
returns entire matching parent tags. why code supposed correct?
print(soup.a.string.find_parent("p"))
given :
<p> <a>"...."</a> </p>
p isn't immediate parent of string.
parents
means ancestor, find_parent("p")
means find first ancestor name p
in [5]: soup.a.string.parents out[5]: <generator object parents @ 0x7f2ee14558e0> in [6]: list(_) out[6]: [<a>"...."</a>, <p> <a>"...."</a> </p>, <body><p> <a>"...."</a> </p></body>, <html><body><p> <a>"...."</a> </p></body></html>, <html><body><p> <a>"...."</a> </p></body></html>] in [7]: in soup.a.string.parents: ...: if a.name == "p": ...: print(a) ...: break ...: <p> <a>"...."</a> </p>
Comments
Post a Comment