Topic Overview

Welcome! In this lesson, we'll delve into advanced link navigation and URL management within the realm of web scraping using Python and BeautifulSoup. Our goal is to ensure that you can navigate between linked web pages and manage URLs effectively for scalable web scraping.

Navigating Author Details

To solidify your understanding of link navigation, we'll focus on a scenario where you scrape quotes from a website and navigate to author pages to extract additional information. This process involves extracting links from the main page, navigating to the linked pages, and scraping data from those pages. The following code scrapes quote from the main page and navigate to the author pages for more information:

import requests
from bs4 import BeautifulSoup

def scrape_quotes(base_url):
    response = requests.get(base_url)
    soup = BeautifulSoup(response.text, 'html.parser')

    quotes = soup.select('.quote')

    for quote in quotes:
        text = quote.select_one('.text').get_text()
        author = quote.select_one('.author').get_text()
        print(f'{text} - {author}')

        endpoint_to_about_page = quote.select_one('span a')['href']
        url_to_about_page = base_url + endpoint_to_about_page

        response = requests.get(url_to_about_page)
        soup_about = BeautifulSoup(response.text, 'html.parser')
        born_date = soup_about.select_one('.author-born-date').get_text()
        born_location = soup_about.select_one('.author-born-location').get_text()
        print(f'{author} was born on {born_date} in {born_location}\n')

base_url = 'http://quotes.toscrape.com'
scrape_quotes(base_url)
  1. First, we import the necessary libraries and define a soup object for the main page.

  2. Then, we extract quotes from the main page and iterate over each quote to extract text and author information.

  3. After that, we extract the endpoint to the author's page and construct the full URL:

    endpoint_to_about_page = quote.select_one('span a')['href']
    url_to_about_page = base_url + endpoint_to_about_page

    Remember that select_one() returns the first matching element, and we use the ['href'] attribute to extract the endpoint.

  4. Once we have the full URL, we send a request to the author's page and create a new soup object to extract additional information:

    response = requests.get(url_to_about_page)
    soup_about = BeautifulSoup(response.text, 'html.parser')
    born_date = soup_about.select_one('.author-born-date').get_text()
    born_location = soup_about.select_one('.author-born-location').get_text()
    print(f'{author} was born on {born_date} in {born_location}\n')

    Notice, that in this snippet as well, we use select_one() to extract the birth date and location of the author.

The output of the code will be the following:

“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.” - Albert Einstein
Albert Einstein was born on March 14, 1879 in in Ulm, Germany

“It is our choices, Harry, that show what we truly are, far more than our abilities.” - J.K. Rowling
J.K. Rowling was born on July 31, 1965 in in Yate, South Gloucestershire, England, The United Kingdom

“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.” - Albert Einstein
Albert Einstein was born on March 14, 1879 in in Ulm, Germany

“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.” - Jane Austen
Jane Austen was born on December 16, 1775 in in Steventon Rectory, Hampshire, The United Kingdom
...
Lesson Summary and Practice

In this lesson, we've covered advanced link navigation and URL management in web scraping using Python and BeautifulSoup. We examined and extracted links, navigated between pages, handled relative and absolute URLs, and applied these concepts in a detailed code example. These skills will enable you to handle more complex web scraping tasks effectively.

These exercises will help you practice and deepen your understanding of link navigation and URL management in web scraping, enhancing your proficiency in scalable scraping projects. Happy Scraping!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal