How do I use BeatifulSoup4 to get the raw HTML of a specific div element

I’m not sure what function would get the raw HTML of a specific div element using BeatifulSoup4. Anyone know how?

Hi @RedCoder !
Phind helped me a while back:

from bs4 import BeautifulSoup
import requests

def scrape_website(url):
    # Send a GET request to the website
    response = requests.get(url)

    # If the GET request is successful, the status code will be 200
    if response.status_code == 200:
        # Get the content of the response
        webpage = response.text

        # Create a BeautifulSoup object and specify the parser
        soup = BeautifulSoup(webpage, 'html.parser')

        # Now you can navigate the HTML tree, for example getting all links:
        for link in soup.find_all('a'):

# Use the function

Hope this helps!

1 Like

@NateDhaliwal I’d like to get the raw HTML of all the child’s of a

element. How would I do this?

ChatGPT has kindly given us a possible solution:

To get the raw HTML of an element on a web page using BeautifulSoup (bs4) in Python, you can follow these steps:

  1. First, you need to install BeautifulSoup if you haven’t already. You can use pip for this:
pip install beautifulsoup4
  1. Import the necessary libraries:
from bs4 import BeautifulSoup
import requests
  1. Fetch the HTML content of the webpage:
url = ''  # Replace with the URL of the webpage you want to scrape
response = requests.get(url)
html = response.text
  1. Create a BeautifulSoup object to parse the HTML:
soup = BeautifulSoup(html, 'html.parser')
  1. Find the specific element you want. For example, if you want to get the raw HTML of an <a> element with a specific class:
element = soup.find('a', class_='your-class-name')  # Replace 'your-class-name' with the actual class name of the element
  1. Get the raw HTML of the found element:
if element:
    raw_html = str(element)
    print("Element not found.")

This code will fetch the HTML content of the specified webpage, parse it using BeautifulSoup, find the element you want based on its tag and attributes, and then print the raw HTML of that element. Make sure to replace '' with the actual URL of the page you want to scrape and 'your-class-name' with the class name or other attributes of the element you’re interested in.


Lol. I loved how ChatGPT solved the problem.

Anyways, thank you for the help!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.