-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalyzing News Headlines_A Web Scraping and Data Visualization Project Using Python.py
More file actions
59 lines (48 loc) · 1.6 KB
/
Analyzing News Headlines_A Web Scraping and Data Visualization Project Using Python.py
File metadata and controls
59 lines (48 loc) · 1.6 KB
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
49
50
51
52
53
54
55
56
57
58
59
import requests
from bs4 import BeautifulSoup
import json
import datetime
from urllib.parse import urljoin
# ---------------------------
# Settings
# ---------------------------
url = 'https://www.abc.net.au/news'
keywords = ['Australia', 'Australian', 'Australians'] # adjust if needed
output_file = f'headlines_{datetime.datetime.now().strftime("%Y-%m-%d")}.json'
headers = {'User-Agent': 'Mozilla/5.0'}
# ---------------------------
# Fetch Website
# ---------------------------
response = requests.get(url, headers=headers)
response.raise_for_status()
# ---------------------------
# Parse HTML
# ---------------------------
soup = BeautifulSoup(response.text, 'html.parser')
news_items = []
seen_links = set()
# ---------------------------
# Extract Headlines
# ---------------------------
for article in soup.find_all('a', href=True):
headline = article.get_text(strip=True)
link = urljoin(url, article['href'])
if not headline or link in seen_links:
continue
if any(k.lower() in headline.lower() for k in keywords):
news_items.append({
'headline': headline,
'link': link
})
seen_links.add(link)
# ---------------------------
# Save JSON
# ---------------------------
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(news_items, f, indent=4, ensure_ascii=False)
# ---------------------------
# Output
# ---------------------------
print(f"\nSaved {len(news_items)} news items to {output_file}\n")
for item in news_items:
print(f"{item['headline']}\n{item['link']}\n")