问题
With BeautifulSoup, how to access to a <li>
which has a specific div
as child?
Example: How to access to the text (i.e. info@blah.com) of the li
which has Email
as child div?
<li>
<div>Country</div>
Germany
</li>
<li>
<div>Email</div>
info@blah.com
</li>
I tried to do it manually: looping on all li
, and for each of them, relooping on all child div
to check if text is Email, etc. but I'm sure there exists a more clever version with BeautifulSoup.
回答1:
There are multiple ways to approach the problem.
One option is to locate the Email
div by text and get the next sibling:
soup.find("div", text="Email").next_sibling.strip() # prints "info@blah.com"
回答2:
Your Question is about the get the whole <li>
part which has "Email" inside the <div>
tag right? Meaning you need to get the following result,
<li>
<div>Email</div>
info@blah.com
</li>
If I am understanding you question correctly means you need to do the following thing.
soup.find("div", text="Email").parent
or if you need "info@blah.com" as your result you need to do the following thing.
soup.find("div", text="Email").next_sibling
回答3:
If you have only a single div has content "Email", you can do this way.
soup.find("div", text="Email").find_parent('li')
来源:https://stackoverflow.com/questions/34295451/find-elements-which-have-a-specific-child-with-beautifulsoup