新聞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 3import 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 3from 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()
產出: