Searching in Beautiful Soup
Beautiful Soup helps in scraping information from web pages. Useful information is scattered across web pages as text or attribute values of different tags. In order to scrape such pages, it is necessary to search through the entire page for different tags based on the attribute values or tag name or texts within the document. To facilitate this, Beautiful Soup comes with inbuilt search methods listed as follows:
find()
find_all()
find_parent()
find_parents()
find_next_sibling()
find_next_siblings()
find_previous_sibling()
find_previous_siblings()
find_previous()
find_all_previous()
find_next()
find_all_next()
Searching with find()
In this chapter, we will use the following HTML code for explaining the search using Beautiful Soup. We can save this as an HTML file named ecologicalpyramid.html
inside the Soup
directory we created in the previous chapter.
<html> <body> <div class="ecopyramid"> <ul id="producers"> <li...