1 Star 0 Fork 1

Jeremy Lee/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Google_News.py 1003 Bytes
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 14:22 . Reformat Code by PyCharm-Community
import ssl
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
def news(xml_news_url):
'''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")
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("+-" * 20, "\n\n")
# 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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Jelmy/Python-1.git
[email protected]:Jelmy/Python-1.git
Jelmy
Python-1
Python-1
master

搜索帮助