如何将这个 curl 命令转换为一些 Python 代码来做同样的事情?
how to convert this curl command to some Python codes that do the same thing?
我正在尝试使用 Fitbit API 下载我的数据。想通了如何获取某一天的数据,很好。这是我使用的 curl 命令:
curl -i -H "Authorization: Bearer (here goes a very long token)" https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json >> heart_rate_20160614.json
但是,我想收集数百天的数据,我不想手动进行。所以我想我可以使用 Python 循环。我阅读了一些其他主题,例如 this one and this one,但仍然不知道如何使用 urllib2 将这些 curl 命令 'translate' 转换为 python 语言。
我试过这个:
import urllib2
url = 'https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/1sec/time/00:00/00:01.json'
data = '{Authorization: Bearer (here goes a very long token)}'
req = urllib2.Request(url,data)
f = urllib2.urlopen(req)
但是出现错误 "HTTP Error 404: Not Found"
那么 'translate' 这个 curl 命令到 python 语言的正确方法是什么?谢谢!
问题来自 Request
object 的构造:默认情况下,第二个参数是您要随请求一起传递的数据。相反,您必须指定要传递 headers。这是正确的做法:
import urllib2
url = 'https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json'
hdr = {'Authorization': 'Bearer (token)'}
req = urllib2.Request(url,headers=hdr)
f = urllib2.urlopen(req)
这对我来说是 401,但应该适用于您的令牌。
您可以获得有关 urllib2(和请求 class)的更多信息here
但是,我建议您看一下 Requests,我认为它更容易使用,而且有很好的文档记录。
希望对您有所帮助。
你可以使用优秀的库 requests
,在我看来它比 urllib
更容易使用。
首先,pip install requests
,然后在您的解释器中:
import requests
response = requests.get(url='https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json', headers={'Authorization':'Bearer <TOKEN>'})
if response.ok:
print response.content
else:
print "error", response.content
从这里您可以轻松地通过 response.content
或 response.json()
获取响应内容(如果它是 JSON,并将其写入文件。
我正在尝试使用 Fitbit API 下载我的数据。想通了如何获取某一天的数据,很好。这是我使用的 curl 命令:
curl -i -H "Authorization: Bearer (here goes a very long token)" https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json >> heart_rate_20160614.json
但是,我想收集数百天的数据,我不想手动进行。所以我想我可以使用 Python 循环。我阅读了一些其他主题,例如 this one and this one,但仍然不知道如何使用 urllib2 将这些 curl 命令 'translate' 转换为 python 语言。
我试过这个:
import urllib2
url = 'https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/1sec/time/00:00/00:01.json'
data = '{Authorization: Bearer (here goes a very long token)}'
req = urllib2.Request(url,data)
f = urllib2.urlopen(req)
但是出现错误 "HTTP Error 404: Not Found"
那么 'translate' 这个 curl 命令到 python 语言的正确方法是什么?谢谢!
问题来自 Request
object 的构造:默认情况下,第二个参数是您要随请求一起传递的数据。相反,您必须指定要传递 headers。这是正确的做法:
import urllib2
url = 'https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json'
hdr = {'Authorization': 'Bearer (token)'}
req = urllib2.Request(url,headers=hdr)
f = urllib2.urlopen(req)
这对我来说是 401,但应该适用于您的令牌。
您可以获得有关 urllib2(和请求 class)的更多信息here
但是,我建议您看一下 Requests,我认为它更容易使用,而且有很好的文档记录。
希望对您有所帮助。
你可以使用优秀的库 requests
,在我看来它比 urllib
更容易使用。
首先,pip install requests
,然后在您的解释器中:
import requests
response = requests.get(url='https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json', headers={'Authorization':'Bearer <TOKEN>'})
if response.ok:
print response.content
else:
print "error", response.content
从这里您可以轻松地通过 response.content
或 response.json()
获取响应内容(如果它是 JSON,并将其写入文件。