热线电话:13121318867

登录
2019-04-11 阅读量: 1020
python如何使用News API获取热门新闻

可以根据某些标准检索新闻。假设要搜索的主题(关键字)是'Geeksforgeeks'或者可能与特定频道有关。一切都可以完成,但需要API Key才能开始。

# 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])

# Driver Code

if __name__ == '__main__':

# function call

NewsFromBBC()

输出 :

1 China hits back with tariffs on US imports
2 Trump rails at immigrant programme
3 'I have 29 textbooks for 87 pupils'
4 French rail strike set to test Macron
5 London murder rate overtakes New York's
6 Hill Street Blues creator dies aged 74
7 Villagers killed in Boko Haram clash
8 Kim 'moved' by K-pop peace concert
9 Former president's poisoning ordeal
10 Costa Rican president-elect wants 'unity'
0.0000
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子