From 6c9a92f21671dba1884ed1c5284818eb94f80079 Mon Sep 17 00:00:00 2001 From: AryanPrajapati9456 Date: Sun, 8 Feb 2026 00:41:25 +0530 Subject: [PATCH 1/2] Improve error handling, fix counter logic, and prevent variable shadowing in news function --- Google_News.py | 59 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/Google_News.py b/Google_News.py index c63ffacaaab..f93231db4fe 100644 --- a/Google_News.py +++ b/Google_News.py @@ -3,37 +3,64 @@ 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 """ - context = ssl._create_unverified_context() - Client = urlopen(xml_news_url, context=context) - xml_page = Client.read() - Client.close() + 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") + i = 0 # counter to print n number of news items - for news in news_list: - print(f"news title: {news.title.text}") # to print title of the news - print(f"news link: {news.link.text}") # to print link of the news - print(f"news pubDate: {news.pubDate.text}") # to print published date + 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 i == counter: - break i = i + 1 -# 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" +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) + # now call news function with any of these url or BOTH + news(news_url, 10) + news(sports_url, 5) From cf1ee3d1fcd8c840edacddd974b0e1ab2ca2bf2b Mon Sep 17 00:00:00 2001 From: Depthstrider Date: Sun, 8 Feb 2026 00:54:53 +0530 Subject: [PATCH 2/2] Refactor news function by removing unused counter Removed redundant counter variable and comments in news function. --- Google_News.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Google_News.py b/Google_News.py index f93231db4fe..3de879b3c01 100644 --- a/Google_News.py +++ b/Google_News.py @@ -22,7 +22,7 @@ def get_text_or_default(tag, default="N/A"): return tag.text if tag else default -# --- news printing function--- +# --- News printing function--- def news(xml_news_url, counter): @@ -38,8 +38,6 @@ def news(xml_news_url, counter): news_list = soup_page.findAll("item") - i = 0 # counter to print n number of news items - for i, item in enumerate(news_list): if i >= counter: break @@ -53,8 +51,6 @@ def news(xml_news_url, counter): print(f"news pubDate: {pub_date}") print("+-" * 20, "\n\n") - i = i + 1 - if __name__ == "__main__": # you can add google news 'xml' URL here for any country/category