新闻API是用于搜索和检索来自整个Web的实时新闻文章,可以根据某些标准检索新闻。
使用它,可以获取任何新闻网站上运行的*新闻,也可以搜索特定主题(或关键字)的*新闻。
假设要搜索的主题(关键字)是“geeksabiek”,或者可能与某个特定的频道有关。所有这些都可以完成,但是需要API密钥才能开始。
以下是上述想法的实施情况:
# importing requests package
import requests
def NewsFromBBC():
# BBC news api
main_url = " https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=4dbc17e007ab436fb66416009dfb59a8"
# fetching data in json format
open_bbc_page = requests.get(main_url).json()
# getting all articles in a string article
article = open_bbc_page["articles"]
# empty list which will
# contain all trending news
results = []
for ar in article:
results.append(ar["title"])
for i in range(len(results)):
# printing all trending news
print(i + 1, results[i])
#to read the news out loud for us
from win32com.client import Dispatch
speak = Dispatch("SAPI.Spvoice")
speak.Speak(results)
# Driver Code
if __name__ == '__main__':
# function call
NewsFromBBC()
产出:
1 Italy to lift coronavirus travel restrictions
2 White House 'Operation Warp Speed' to look for Covid jab
3 Two Americas in the nation's capital
4 Kobe Bryant helicopter crash post-mortem released
5 Little things people are doing while socially distanced
6 The last 'normal' photo on your phone
7 'They came to kill the mothers'
8 EU-UK Brexit trade talks in trouble
9 Trial starts to see if dogs can 'sniff out' virus
10 Beatles photographer Astrid Kirchherr dies aged 81
注:输出可能会根据时间的前几篇而改变
案例:GoogleNewsFeed
先决条件——Python tkinter
在本文中,我们将编写一个python脚本,从GoogleNewsFeed中提取新闻文章用Gnewsclient模块,把它绑在一起a GUI申请。Gnewsclient是GoogleNewsFeed的python客户端。为了使用这个API,必须先显式地安装这个API。
安装
下面的终端命令安装gnewsclient包及其所有必需的库。运行就行了。
pip install gnewsclient
使用模块
- 导入gnewsclient模块
- 创建NewsClient对象并设置当前参数设置
- 获取新闻信息
Python 3
# import module
from gnewsclient import gnewsclient
# declare a NewsClient object
client = gnewsclient.NewsClient(language='hindi', location='india', topic='Business', max_results=5)
# get news feed
client.get_news()
产出:
下面的代码描述了如何从本模块收集的信息中打印其他因素,如位置、语言和主题:
Python 3
import gnewsclient
from gnewsclient import gnewsclient
client = gnewsclient.NewsClient(language='hindi',
location='india',
topic='Business',
max_results=5)
# prints location
print("Location: \n",client.locations)
print()
# prints languages
print("Language \n",client.languages)
print()
# prints topics
print("Topic \n",client.topics)
产出:
案例:
Python 3
from gnewsclient import gnewsclient
client = gnewsclient.NewsClient(language='english',
location='india',
topic='sports',
max_results=3)
news_list = client.get_news()
for item in news_list:
print("Title : ", item['title'])
print("Link : ", item['link'])
print("")
产出:
程序2该代码在GUI中实现了程序1的方法。
Python 3
# import modules
from tkinter import *
from gnewsclient import gnewsclient
# defined funtions
def news():
client = gnewsclient.NewsClient(
language=lang.get(), location=loc.get(), topic=top.get(), max_results=3)
news_list = client.get_news()
result_title.set(news_list[0]["title"] + "\n" +
news_list[1]["title"] + "\n" + news_list[2]["title"])
# tkinter object
master = Tk()
master.title("NEWS")
# background set to grey
master.configure(bg='light grey')
# Variable Classes in tkinter
result_title = StringVar()
result_link = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Choose language :", bg="light grey").grid(row=0, sticky=W)
Label(master, text="Choose Location :", bg="light grey").grid(row=1, sticky=W)
Label(master, text="Choose Topic :", bg="light grey").grid(row=2, sticky=W)
lang = Entry(master)
lang.grid(row=0, column=1)
loc = Entry(master)
loc.grid(row=1, column=1)
top = Entry(master)
top.grid(row=2, column=1)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=result_title,
bg="light grey").grid(row=3, column=1, sticky=W)
# creating a button using the widget
# Button to call the submit function
Button(master, text="SHOW", command=news, bg="white").grid(row=1, column=3)
mainloop()
产出: