File size: 1,718 Bytes
7083c8e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
####
''' A simple web scraping script using requests and BeautifulSoup libraries. I am trying to achive the following:
1. Send an HTTP GET request to a website.
2. Parse the HTML content of the page.
3. Extract relevant data (e.g., tables, headings, paragraphs).
4. Save the extracted data to a file or display it in the terminal.'''
import requests
from bs4 import BeautifulSoup
# URL of the website to scrape
url = "https://www.ireland.ie/en/india/newdelhi/services/visas/processing-times-and-decisions/"
# Headers to mimic a browser request
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
)
}
# Send an HTTP GET request to the website with headers
response = requests.get(url, headers=headers)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the HTML content of the page
soup = BeautifulSoup(response.content, 'html.parser')
# Extract relevant data (e.g., tables, headings, paragraphs)
paragraphs = soup.find_all('p')
for i, paragraph in enumerate(paragraphs, start=1):
print(f"Paragraph {i}: {paragraph.get_text(strip=True)}")
print("-" * 80)
# Example: Scraping tables (if there are any)
tables = soup.find_all('table')
for table in tables:
print("\nTable found:")
rows = table.find_all('tr')
for row in rows:
cells = row.find_all(['th', 'td'])
cell_data = [cell.get_text(strip=True) for cell in cells]
print("\t".join(cell_data))
else:
print(f"Failed to retrieve the webpage. Status code: {response.status_code}")
|