1 Star 0 Fork 0

dzc/Python-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Google_News.py 1018 Bytes
一键复制 编辑 原始数据 按行查看 历史
cclauss 提交于 2019-01-07 18:01 +08:00 . Fix IndentationError: unexpected indent
import bs4
import lxml #xml parser
import ssl
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
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/sdredboy/Python-1.git
git@gitee.com:sdredboy/Python-1.git
sdredboy
Python-1
Python-1
master

搜索帮助