Web Scraping With Python And Beautiful Soup

Web Scraping with Python and Beautiful Soup

In the world of data science, gathering and preprocessing data is half the battle. Cutting-edge algorithms are useless without high-quality input data, a process involving significant effort and time. In many circumstances, the required data isn’t readily accessible in a nicely prepped CSV file or a well-maintained database—it’s out there in the broad expanse of the internet. That’s where web scraping, a process of extracting information from websites, comes in. One of the most popular Python libraries for web scraping is Beautiful Soup.

Web Scraping With Python And Beautiful Soup
Web Scraping With Python And Beautiful Soup

In this tutorial, we will cover an introduction to web scraping, explain how to use the Beautiful Soup library in Python, and work on real-life examples.

Table of Contents

  1. What is Web Scraping?
  2. Introduction to Beautiful Soup
  3. Getting Started with Beautiful Soup
  4. Advanced Web Scraping with Beautiful Soup
  5. Best Practices and Python Web Scraping Legalities

1. What is Web Scraping?

Web Scraping is a methodology used to extract large amounts of data from websites where the data is extracted and saved to a local file in your computer or to a database in table (tabular) format.

Why Web Scraping? The Data available on the websites is unstructured. Web scraping enables us to convert that data into structured form.

Python, a high-level interpreted programming language, provides libraries for efficient web scraping, one of which is Beautiful Soup.

2. Introduction to Beautiful Soup

Beautiful Soup is a Python library that is used for web scraping purposes to pull the data out of HTML and XML files. It creates a parse tree from page source code that can be used to extract data in a hierarchical and more readable manner.

Note: Beautiful Soup automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You don’t have to think about encodings, unless the document doesn’t specify an encoding and Beautiful Soup can’t detect one. Then you just have to mention the original encoding.

Beautiful Soup sits on top of the parser and provides Python-friendly representations of the parse tree and the data therein.

3. Getting Started with Beautiful Soup

To start using Beautiful Soup, you first need to install it. This can be done easily with pip:

pip3 install beautifulsoup4

Also, you will need the requests library to make HTTP requests in Python.

pip3 install requests

Basic usage with Beautiful Soup

First, we import the libraries:

from bs4 import BeautifulSoup
import requests

Then, we fetch a webpage that we will parse:

response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'html.parser')

The ‘html.parser’ argument tells Beautiful Soup to use Python’s built-in HTML parser.

From here, you can extract many types of information/data from the fetched webpage, such as title, all links, specific elements, etc. Here are some examples:

Extracting the Title:

print(soup.title)

Extracting all the URLs found within a page’s < a > tags:

for link in soup.find_all('a'):
    print(link.get('href'))

Extracting all the text of a page:

print(soup.get_text())

4. Advanced Web Scraping with Beautiful Soup


Navigating the Parse Tree

Once a document is parsed and Beautiful Soup object is created, we can parse this object and find specific elements using find_all(), find(), select_one(), select() methods:

find_all() is a method that finds all the tags and returns a list containing the tags.

print(soup.find_all('b'))   # This will return all the 'b' tags
find() returns the first matching tag only:

print(soup.find('b'))   # This will return the first 'b' tag

An important note: If you can’t find a tag that should be there, Beautiful Soup may be parsing your document as XML when you don’t want it to.

Web Scraping Example

Here, we will scrape top articles from PythonTimes.com with their titles, links and publishing date. A fairly simple and illustrative example to practice.

from bs4 import BeautifulSoup
import requests

base_site = 'https://www.pythontimes.com'

response = requests.get(base_site)
soup = BeautifulSoup(response.text, 'html.parser')
articles = soup.find_all('article')

titles = [article.h2.a.text for article in articles]
links = [article.h2.a['href'] for article in articles]
published_dates =[article.time.text for article in articles]

for i in range(len(titles)):
    print(f"{titles[i]} \n{published_dates[i]} \n{links[i]}\n---")

5. Best Practices and Python Web Scraping Legalities

Always remember that with great power comes great responsibility. Web scraping, while extremely powerful and useful, must not be used indiscriminately. A large number of request hits to a website might result in our IP being blocked, or legal actions might be taken against us. Here are some points to remember:

  • Always respect the website’s robots.txt file and abide by the scraping policies
  • Allow gaps between our scraping requests not to overload the server
  • Use the API if the website provides one instead of scraping
  • Web scraping is a grey area legally. Always consult with a lawyer if unsure about the legality of your scraping operation

Conclusion

In this tutorial, you’ve learned about web scraping, Beautiful Soup, how to extract data from a webpage using Beautiful Soup, and finally, some best practices and legalities.

The world of Python web scraping is vast with many more libraries such as Selenium, Scrapy, etc. Beautiful Soup just provides an excellent place to start off because of its simplicity and ease of use. We hope this tutorial provided a good introduction and starting point for you in web scraping with Python and Beautiful Soup.

Happy scraping, and remember, be responsible, respect privacy and terms of use while scraping, and always believe in the power of data-driven decision making!


References: 1. Beautiful Soup official documentation
2. Request Library documentation
3. Web Scraping Legal Guidelines

Share this article:

Leave a Comment