VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-extract-a-div-tag-and-its-contents-by-id-with-beautifulsoup/

⇱ How to extract a div tag and its contents by id with BeautifulSoup? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to extract a div tag and its contents by id with BeautifulSoup?

Last Updated : 3 Mar, 2021

Beautifulsoup is a Python library used for web scraping. This powerful python tool can also be used to modify HTML webpages. This article depicts how beautifulsoup can be employed to extract a div and its content by its ID. For this, find() function of the module is used to find the div by its ID.

Approach:

  • Import module
  • Scrap data from a webpage
  • Parse the string scraped to HTML
  • Find the div with its ID
  • Print its content

Syntax : find(tag_name, **kwargs)

Parameters:

  • The tag_name argument tell Beautiful Soup to only find tags with given names. Text strings will be ignored, as will tags whose names that don’t match.
  • The **kwargs arguments are used to filter against each tag’s ‘id’ attribute.

Below is the implementation:

Example 1:

Output:

Div Content

Example 2:

Output:

 Div with id second
Comment