-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeadlines Web Scaper.py
More file actions
29 lines (24 loc) · 847 Bytes
/
Headlines Web Scaper.py
File metadata and controls
29 lines (24 loc) · 847 Bytes
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
import requests
from bs4 import BeautifulSoup
# Set the URL of the web page form which you want to scrape the news headlines
url = "http://www.example.com"
# Fetch the web page
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the HTML content
soup = BeautifulSoup(response.text, "html.parser")
# Find all of the div elements on the page
divs = soup.find_all("div")
# Check if any div elements were found
if divs:
# Save the data to a file or database
with open("data.txt", "w") as f:
for div in divs:
f.write(div.text + "\n")
else:
# No div elements were found, handle the error
print("No div elements were found.")
else:
# Request was not successful, handle the error
print(f"Request failed with status code {response.status_code}.")