Find elements which have a specific child with BeautifulSoup

好久不见. 提交于 2021-01-23 04:49:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!