首页 > 解决方案 > 如何在 Python 中使用 API 密钥进行 API 请求

问题描述

所以我一直在尝试访问这个 API,但是我需要使用 API 密钥来访问它。我有一个 API 密钥,但我不确定如何格式化所有内容并传输 url。我的网址看起来像这样--- https://api.sportsdata.io/v3/nfl/scores/json/Players

我正在使用的坐席也留下了这条信息:

This is the documentation for SportsDataIO's NFL API. All of our API endpoints can be accessed via an HTTP GET request using your API key. The API key can be passed either as a query parameter or using the following HTTP request header.
Ocp-Apim-Subscription-Key: {key}

不知道这意味着什么,我对数据抓取很陌生。

如果有人可以提供帮助,那就太好了,谢谢。

标签: listapidictionarykeyscreen-scraping

解决方案


意味着您需要通过标头传递 api 密钥:

import requests

api_key = 'put your api key here'

url = 'https://api.sportsdata.io/v3/nfl/scores/json/Players'
headers = {'Ocp-Apim-Subscription-Key': '{key}'.format(key=api_key)}

jsonData = requests.get(url, headers=headers).json()

推荐阅读