Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从API获取数据并使用Python将其保存到txt_Python_Api_Get - Fatal编程技术网

从API获取数据并使用Python将其保存到txt

从API获取数据并使用Python将其保存到txt,python,api,get,Python,Api,Get,我需要从这个API(示例节点)获取数据 这是我的代码(python): 导入请求 f=请求。获取('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434') 打印f.text 我只想在txt文件的后续三行中保存协议、响应时间和信誉。它应该看起来像这样: protocol: 1.2.0 responseTime: 8157.912472694088 reputation: 1377 协议:1.2.0 答复时间:

我需要从这个API(示例节点)获取数据

这是我的代码(python):

导入请求 f=请求。获取('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434') 打印f.text 我只想在txt文件的后续三行中保存协议、响应时间和信誉。它应该看起来像这样:

protocol: 1.2.0 responseTime: 8157.912472694088 reputation: 1377 协议:1.2.0 答复时间:8157.912472694088 声誉:1377
不幸的是,我被困在这一点上,我无法以任何方式处理这些数据

这是一种非常粗制滥造的方式来做你想做的事情,你可以用它来构建。您需要为text.txt插入路径/文件名

import requests
import json

f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')
t = json.loads(f.text)

with open('text.txt', 'a') as mfile:
  mfile.write("protocol: {0}".format(str(t['protocol'])))
  mfile.write("responseTime: {0}".format(str(t['responseTime'])))
  mfile.write("reputation: {0}".format(str(t['reputation'])))

这是一种非常粗制滥造的方法,你可以用它来做你想做的事情。您需要为text.txt插入路径/文件名

import requests
import json

f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')
t = json.loads(f.text)

with open('text.txt', 'a') as mfile:
  mfile.write("protocol: {0}".format(str(t['protocol'])))
  mfile.write("responseTime: {0}".format(str(t['responseTime'])))
  mfile.write("reputation: {0}".format(str(t['reputation'])))
希望有帮助!干杯


希望有帮助!干杯

您只需转换为JSON对象即可访问密钥

import requests
import simplejson as json

f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')

x = json.loads(f.text)

print 'protocol: {}'.format(x.get('protocol'))
print 'responseTime: {}'.format(x.get('responseTime'))
print 'reputation: {}'.format(x.get('reputation'))

您只需要转换为JSON对象就可以访问密钥

import requests
import simplejson as json

f = requests.get('https://api.storj.io/contacts/f52624d8ef76df81c40853c22f93735581071434')

x = json.loads(f.text)

print 'protocol: {}'.format(x.get('protocol'))
print 'responseTime: {}'.format(x.get('responseTime'))
print 'reputation: {}'.format(x.get('reputation'))