Skip to content

Commit

Permalink
Update Google_News.py
Browse files Browse the repository at this point in the history
Used a variable counter to list the number of news a user want to see otherwise it shows more than 30+ news items at a time.
  • Loading branch information
Prince326 committed Apr 16, 2020
1 parent cfe9491 commit c2baf91
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Google_News.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from bs4 import BeautifulSoup as soup


def news(xml_news_url):
def news(xml_news_url,counter):
'''Print select details from a html response containing xml
@param xml_news_url: url to parse
'''
Expand All @@ -17,18 +16,22 @@ def news(xml_news_url):
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}')
print(f'news link: {news.link.text}')
print(f'news pubDate: {news.pubDate.text}')
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
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"

# now call news function with any of these url or BOTH
news(news_url)
news(sports_url)
news(news_url,10)
news(sports_url,5)

0 comments on commit c2baf91

Please sign in to comment.