-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgoogleUpdateNews.py
40 lines (29 loc) · 1.04 KB
/
googleUpdateNews.py
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
import ssl
from urllib.request import urlopen
"""
for information about beautifulsoup or how to install check on:
https://pypi.org/project/beautifulsoup4/
"""
from bs4 import BeautifulSoup as soup
def news(xmlNewsUrl, counter):
context = ssl._create_unverified_context()
client = urlopen(xmlNewsUrl, context=context)
xmlPage = client.read()
client.close()
soupPage = soup(xmlPage, "xml")
newsList = soupPage.findAll("item")
i = 0
for news in newsList:
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
else:
i = i + 1
newsUrl = "https://news.google.com/news/rss/?ned=us&gl=US&hl=en"
sportUrl = "https://news.google.com/news/rss/headlines/section/topic/SPORTS.en_in/Sports?ned=in&hl=en-IN&gl=IN"
# print news
news(newsUrl, 10)
news(sportUrl, 5)