Home > Net >  BeautifulSoup - find the element following an already found element
BeautifulSoup - find the element following an already found element

Time:02-11

I am trying to find the ul element under the h2 element.

enter image description here

I find the h2 element with:

tmpSpan = soupDetail.find_all("a", {"class": "external"})
tmpParent = tmpSpan.parent

I have tried several methods to get the following ul element (descendants, next_sibling, next-element, etc. ) but the result is always the span element in the h1.

How can I do this?

CodePudding user response:

tmpSpan = soupDetail.find_all("a", {"class": "external"})
tmpParent = tmpSpan.parent

Above code from your question will lead to an AttributeError, cause ResultSet object has no attribute parent.


Select your tags more specific for example with css selectors - An build it based on tag or id, cause they are mor static then classes.

Get the <h2> based on its content:

soup.select_one('h2:has(span#External_links)')

Get the next sibling <ul> of such these <h2>:

soup.select_one('h2:has(span#External_links)   ul')

or

soup.select_one('h2:has(span#External_links)').find_next('ul')

Example

from bs4 import BeautifulSoup
html='''
<h2>
    <span id="External_links"></span>
    <span>
        <a ></a>
    </span>
</h2>
<ul></ul>
'''

soup = BeautifulSoup(html, 'lxml')

print(soup.select_one('h2:has(span#External_links)   ul'))

##output
<ul></ul>
  • Related