list - 如何在 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}
不知道这意味着什么,我对数据抓取很陌生。
如果有人可以提供帮助,那就太好了,谢谢。
解决方案
意味着您需要通过标头传递 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()
推荐阅读
- php - 使用正则表达式和 xpath 评估来查看每个日期
- astronomy - 在 IRAF 中移动图像
- android - Modifier.width(intrinsicSize.Min/Max) 和 Modifier.wrapContentWidth 有什么区别?
- arrays - 从条件中获取 JSON 值
- javascript - 包括 iframe 链接的圆环图
- javascript - 如何在结尾处修复 eval() 的表达式以用于尾随括号?
- intellij-idea - 想法不会删除注释的不推荐使用的参数
- c# - C#和SQL获取声明的参数数据类型并动态添加参数
- python - Python:遍历列表并返回列表中所有字符串的元组排列?
- html - Laravel 组件未显示