Book Image

Getting Started with Beautiful Soup

By : Vineeth G Nair
Book Image

Getting Started with Beautiful Soup

By: Vineeth G Nair

Overview of this book

Beautiful Soup is a Python library designed for quick turnaround projects like screen-scraping. Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need without writing excess code for an application. It doesn't take much code to write an application using Beautiful Soup. Getting Started with Beautiful Soup is a practical guide to Beautiful Soup using Python. The book starts by walking you through the installation of each and every feature of Beautiful Soup using simple examples which include sample Python codes as well as diagrams and screenshots wherever required for better understanding. The book discusses the problems of how exactly you can get data out of a website and provides an easy solution with the help of a real website and sample code. Getting Started with Beautiful Soup goes over the different methods to install Beautiful Soup in both Linux and Windows systems. You will then learn about searching, navigating, content modification, encoding support, and output formatting with the help of examples and sample Python codes for each example so that you can try them out to get a better understanding. This book is a practical guide for scraping information from any website. If you want to learn how to efficiently scrape pages from websites, then this book is for you.
Table of Contents (15 chapters)
Getting Started with Beautiful Soup
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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">
      &lt...