我写了这样的代码来获取头条新闻:
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])
如何从搜索关键词列表的几个新闻网站页面中提取头条新闻(例如关于冠状病毒或其他特定主题(?
文档显示您可以将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)
头条新闻文档中的更多详细信息
与requests
相同。
你可以把API Key
放在url中,但最好放在头中。Url可以保存在某些日志中。
我使用了文档中的API 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