zweb

content = response.text soup = BeautifulSoup(content, "html.parser") print(soup.prettify()) # Print the formatted HTML of the page

This code fetches the content of a webpage and parses it into a BeautifulSoup object. You can then use BeautifulSoup methods to navigate and extract elements from the HTML.

Extracting Specific Elements

Once you have the HTML content, you can extract specific elements using find() and find_all() methods:

      
        title = soup.find('title')
        print("Page title:", title.string)

        links = soup.find_all('a')
        for link in links:
            print("Link:", link.get('href'))
      
    

This code finds the <title> tag and all <a> tags (links) in the HTML.

Activity

Try It Yourself!

Write a program that scrapes a webpage and extracts all the images (links to <img> tags) on that page.

Quick Quiz

Quick Quiz

  1. What module is used to make HTTP requests in web scraping?
  2. How do you parse HTML content using BeautifulSoup?

Answers: The requests module is used to make HTTP requests. You can parse HTML with BeautifulSoup() and specify the parser, such as html.parser.