Surya_from_1627

WSP7

Oct 2nd, 2026
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4.  
  5. def extract_all_headers(url):
  6.     try:
  7.         headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
  8.         response = requests.get(url, headers=headers)
  9.         response.raise_for_status()  # Raise exception for HTTP errors
  10.  
  11.         soup = BeautifulSoup(response.text, 'html.parser')
  12.  
  13.         print(f"Header tags from: {url}\n")
  14.         for i in range(1, 7):  # For h1 to h6
  15.             headers = soup.find_all(f'h{i}')
  16.             for tag in headers:
  17.                 print(f"<h{i}>: {tag.text.strip()}")
  18.  
  19.     except requests.exceptions.RequestException as e:
  20.         print(f"Error fetching the webpage: {e}")
  21.  
  22. # Run the function
  23. extract_all_headers("https://en.wikipedia.org/wiki/Main_Page")
  24.  
Advertisement
Add Comment
Please, Sign In to add comment