1 Star 0 Fork 0

wdc/Python001

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Google_News.py 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
Prince Gangurde 提交于 2020-04-17 00:08 . Update Google_News.py
import ssl
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
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()
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
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,10)
news(sports_url,5)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ijiwei/Python001.git
git@gitee.com:ijiwei/Python001.git
ijiwei
Python001
Python001
master

搜索帮助