问题描述
我想要一个API查询到python。命令行
I am trying to get an API query into python. The command line
curl --header "Authorization:access_token myToken" https://website.com/id
提供一些json输出。 myToken是一个十六进制变量,始终保持不变。我想从python这个调用,使我可以循环通过不同的id和分析输出。有任何想法吗?在需要认证之前,我使用urllib2。我还看了一下请求模块,但不知道如何做到这一点。
gives some json output. myToken is a hexadecimal variable that remains constant throughout. I would like to make this call from python so that I can loop through different ids and analyze the output. Any ideas? Before authentication was needed I have done that with urllib2. I have also taken a look at the requests module but couldn't figure out how to do that.
非常感谢。
推荐答案
包对于HTTP请求有一个非常好的API,添加一个自定义头像这样(:
The requests package has a very nice API for HTTP requests, adding a custom header works like this (source: official docs:
>>> import requests
>>> response = requests.get(
... 'https://website.com/id', headers={'Authorization': 'access_token myToken'})
要使用外部依赖,使用标准库的urllib2的同样的事情看起来像这样():
If you don't want to use an external dependency, the same thing using urllib2 of the standard library looks like this (source: the missing manual):
>>> import urllib2
>>> response = urllib2.urlopen(
... urllib2.Request('https://website.com/id', headers={'Authorization': 'access_token myToken'})
这篇关于带认证的python请求(access_token)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!