Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- def extract_all_headers(url):
- try:
- 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'}
- response = requests.get(url, headers=headers)
- response.raise_for_status() # Raise exception for HTTP errors
- soup = BeautifulSoup(response.text, 'html.parser')
- print(f"Header tags from: {url}\n")
- for i in range(1, 7): # For h1 to h6
- headers = soup.find_all(f'h{i}')
- for tag in headers:
- print(f"<h{i}>: {tag.text.strip()}")
- except requests.exceptions.RequestException as e:
- print(f"Error fetching the webpage: {e}")
- # Run the function
- extract_all_headers("https://en.wikipedia.org/wiki/Main_Page")
Advertisement
Add Comment
Please, Sign In to add comment