Skip to content Skip to sidebar Skip to footer

Access Next Sibling
  • Element With BeautifulSoup
  • I am completely new to web parsing with Python/BeautifulSoup. I have an HTML that has (part of) the code as follows:
    • Copy

      next_li_element would become None if the page corresponds to the last active li:

      if next_li_element is None:
          # no more pages to go
      

    Solution 2:

    Have you looked at dir(page) or the documentation? If so, how did you miss .find_next_sibling()?

    from bs4 import BeautifulSoup
    import urllib2
    import re
    
    landingPage = urllib2.urlopen('somepage.com').read()
    soup = BeautifulSoup(landingPage)
    
    pageList = soup.find("div", {"id": "pages"})
    
    page = pageList.find("li", {"class": "active"})
    sibling = page.find_next_sibling()
    

    Post a Comment for "Access Next Sibling
  • Element With BeautifulSoup"