使用Python构建一个应用程序以从 Google News Feed 中提取新闻
先决条件Python tkinter
在本文中,我们将编写一个Python脚本,使用gnewsclient模块从 Google News Feed 中提取新闻文章,并将其与 GUI 应用程序绑定。 gnewsclient 是一个用于 Google News Feed 的Python客户端。必须先明确安装此 API 才能使用。
安装
以下终端命令安装 gnewsclient 包及其所有必需的库。所以,人们只需要在他们的终端中运行这个命令。
pip install gnewsclient
使用模块
- 导入 gnewsclient 模块
- 创建一个 NewsClient 对象并设置当前参数设置
- 获取新闻提要
Python3
# 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()
Python3
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)
Python3
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("")
Python3
# import modules
from tkinter import *
from gnewsclient import gnewsclient
# defined functions
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()
输出:
以下代码描述了如何从此模块收集的信息打印其他因素,如位置、语言和主题:
蟒蛇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)
输出:
示例:
方案一:
蟒蛇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 的方法。
蟒蛇3
# import modules
from tkinter import *
from gnewsclient import gnewsclient
# defined functions
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()
输出: