Surya_from_1627

WSP6

Sep 9th, 2026 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. def extract_h1_tag(url):
  5.     try:
  6.         response = requests.get(url)
  7.         response.raise_for_status()  # Raises error for bad status codes
  8.  
  9.         soup = BeautifulSoup(response.text, 'html.parser')
  10.         h1_tag = soup.find('h1')
  11.  
  12.         if h1_tag:
  13.             print(f"<h1> tag content: {h1_tag.text.strip()}")
  14.         else:
  15.             print("No <h1> tag found on the page.")
  16.  
  17.     except requests.exceptions.RequestException as e:
  18.         print(f"Error fetching the webpage: {e}")
  19.  
  20. # Example usage
  21. extract_h1_tag("https://csszengarden.com")
  22.  
Advertisement
Add Comment
Please, Sign In to add comment