Skip to content

Commit d816e08

Browse files
authored
Add files via upload
1 parent 0db6fd0 commit d816e08

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
beautifulsoup4

web_scraper.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
def scrape_titles(url):
5+
try:
6+
response = requests.get(url)
7+
response.raise_for_status() # Check for request errors
8+
9+
soup = BeautifulSoup(response.text, 'html.parser')
10+
11+
# Adjust the selector to match the website's structure
12+
titles = soup.find_all('h2') # Assuming article titles are in <h2> tags
13+
14+
print(f"Found {len(titles)} titles:")
15+
for i, title in enumerate(titles, start=1):
16+
print(f"{i}: {title.get_text(strip=True)}")
17+
18+
except requests.exceptions.RequestException as e:
19+
print(f"Error fetching data from {url}: {e}")
20+
21+
if __name__ == "__main__":
22+
url = input("Enter the URL of the website to scrape: ")
23+
scrape_titles(url)

0 commit comments

Comments
 (0)