-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathGoogle_News.py
More file actions
62 lines (43 loc) · 1.65 KB
/
Google_News.py
File metadata and controls
62 lines (43 loc) · 1.65 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
60
61
62
import ssl
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
# --- Helper Functions for Error handling---
def fetch_xml(url):
"""Fetch XML content safely from a URL."""
try:
context = ssl._create_unverified_context()
with urlopen(url, context=context) as client:
return client.read()
except Exception as e:
print(f"Error fetching URL: {e}")
return None
def get_text_or_default(tag, default="N/A"):
"""Safely extract text from a tag."""
return tag.text if tag else default
# --- News printing function---
def news(xml_news_url, counter):
"""Print select details from a html response containing xml
@param xml_news_url: url to parse
"""
xml_page = fetch_xml(xml_news_url)
if xml_page is None:
return
soup_page = soup(xml_page, "xml")
news_list = soup_page.findAll("item")
for i, item in enumerate(news_list):
if i >= counter:
break
title = get_text_or_default(item.title)
link = get_text_or_default(item.link)
pub_date = get_text_or_default(item.pubDate)
print(f"news title: {title}")
print(f"news link: {link}")
print(f"news pubDate: {pub_date}")
print("+-" * 20, "\n\n")
if __name__ == "__main__":
# you can add google news 'xml' URL here for any country/category
news_url = "https://news.google.com/news/rss/?ned=us&gl=US&hl=en"
sports_url = "https://news.google.com/news/rss/headlines/section/topic/SPORTS.en_in/Sports?ned=in&hl=en-IN&gl=IN"
# now call news function with any of these url or BOTH
news(news_url, 10)
news(sports_url, 5)