如何从Python中的新闻API中获取特定新闻?

2024-05-29 01:43:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了这样的代码来获取头条新闻:

main_url = "https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=ENTER API KEY"
bbc_page = requests.get(main_url).json() 
article = bbc_page["articles"] 
results = [] 

for arr in article: 
    results.append(arr["title"]) 

for i in range(len(results)): 
    print(i + 1, results[i])

如何从搜索关键词列表的几个新闻网站页面中提取头条新闻(例如,关于Corona病毒或其他特定主题)


Tags: 代码inhttpsorgurlformainarticle
1条回答
网友
1楼 · 发布于 2024-05-29 01:43:04

Documentation显示您可以使用q=表示searching query

来自文档的代码,但带有q='corona virus'

from newsapi import NewsApiClient

# Init
newsapi = NewsApiClient(api_key='API_KEY') # fb3a5891a786455bb898f36e92b09f24

# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='corona virus',
                                          sources='bbc-news,the-verge',
                                          category='business',
                                          language='en',
                                          country='us')

# /v2/everything
all_articles = newsapi.get_everything(q='corona virus',
                                      sources='bbc-news,the-verge',
                                      domains='bbc.co.uk,techcrunch.com',
                                      from_param='2017-12-01',
                                      to='2017-12-12',
                                      language='en',
                                      sort_by='relevancy',
                                      page=2)

有关top-headlines的更多详细信息,请参阅文档


requests相同

您可以将API Key放在url中,但最好放在标题中。Url可以保存在一些日志中

我使用了来自documentationAPI Key,它似乎是活动的,但通常您应该在页面上注册并获得自己的API Key

import requests

API_KEY = 'fb3a5891a786455bb898f36e92b09f24'

params = {
    'q': 'corona virus',
    'source': 'bbc-news',
    'sortBy': 'top',
    'language': 'en',
    #'category': 'business',
    #'country': 'us',
    #'apiKey': API_KEY,
}

headers = {
    'X-Api-Key': API_KEY,  # KEY in header to hide it from url
}

url = 'https://newsapi.org/v2/top-headlines'

response = requests.get(url, params=params, headers=headers)
data = response.json()

articles = data["articles"] 
results = [arr["title"] for arr in articles] 

for i, arr in enumerate(results, 1): 
    print(i, arr)

我的结果是:

1 Live updates: CPAC attendee tests positive for coronavirus; Italy considers locking down center of outbreak
2 Trump: Not concerned at all about coronavirus exposure - CNN Video
3 NHL starts closing dressing rooms to media to prevent spread of coronavirus
4 Two Australian Defence Force officers test positive for COVID-19
5 Princess says passenger brought coronavirus on ship; cruise companies to change boarding protocols
6 CPAC Attendee Tests Positive For Coronavirus, Didn't Have Contact With Trump
7 Hobart man who tested positive for coronavirus ignored direction to self-isolate and went to work at hotel
8 Health Minister says its 'not a day for criticism' as Coronavirus GP accuses health minister of 'grandstanding
9 U.S. conservative conference CPAC attendee tests positive for coronavirus
10 Italy poised to seal off north over coronavirus: Live updates
11 Elderly NSW man becomes third Australian to die with coronavirus
12 Coronavirus update: Two Australian Defence Force personnel infected
13 Live updates: CPAC attendee tests positive for coronavirus; Italy considers locking down center of outbreak
14 The divide between those who can buy in bulk, and those who can't
15 Live updates: CPAC attendee tests positive for coronavirus; Italy considers locking down center of outbreak
16 CPAC attendee tested positive for coronavirus
17 Hobart man who tested positive for coronavirus ignored direction to self-isolate and went to work at hotel
18 Coalition pulls multibillion-dollar coronavirus stimulus plan together
19 China January-February exports tumble, imports down as coronavirus batters trade and business
20 U.S. death toll from coronavirus hits 19, New York declares emergency

相关问题 更多 >

    热门问题