w1coding.comAll tutorials

Python Zweb

Python lessons
  1. Environment Setup
  2. Introduction
  3. Control
  4. Control
  5. Loops
  6. Functions
  7. Lists
  8. Dictionary
  9. Classes
  10. Exception
  11. File
  12. Libray
  13. Date
  14. Regula
  15. JSON
  16. Web
  17. Zweb
  18. Apis
  19. Unity
  20. Llampda
  21. Iterators
  22. Generators
  23. Files
  24. Context
  25. Ilogging
  26. Multithreading
  27. Networking
  28. SQLite
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.