我有一个PaperTrail帐户,我正在尝试编写一个访问PaperTrail日志的Python脚本,并将信息作为JSON抓取。这是我目前的尝试,而且很难看 - 我认为在尝试将Python2转换为Python3时我犯了错误,而且我对API / JSON的理解也有些不清楚。
import http.client, urllib, time, os, json
PAPERTRAIL_TOKEN = "[xxx]"
INTERVAL = 10 * 60
conn = http.client.HTTPSConnection(host = 'papertrailapp.com')
conn.request(
method = 'GET',
url = '/api/v1/events/search.json'
headers = {'X-Papertrail-Token' : os.environ['PAPERTRAIL_TOKEN']})
response = conn.getresponse()
答案 0 :(得分:0)
我对您的计划进行了一些小改动:
#!/usr/bin/env python3
,
行的末尾添加了url
以更正语法 PAPERTRAIL_TOKEN = "[xxx]"
未使用 - 程序在环境中查找此内容,因此请务必在运行前设置:export PAPERTRAIL_TOKEN=xxx
#!/usr/bin/env python3
import http.client, urllib, time, os, json
PAPERTRAIL_TOKEN = "[xxx]"
INTERVAL = 10 * 60
conn = http.client.HTTPSConnection(host = 'papertrailapp.com')
conn.request(
method = 'GET',
url = '/api/v1/events/search.json',
headers = {'X-Papertrail-Token' : os.environ['PAPERTRAIL_TOKEN']})
response = conn.getresponse()
print(json.dumps(json.loads(response.read()), indent=4))