假设我在Python中有一个看起来像的命令
command = 'curl ...etc" > result.json'
subprocess.call(command, shell = True)
file = open("result.json").read()
Run Code Online (Sandbox Code Playgroud)
它现在做的是从我卷曲的地方获取GET并将结果存储在result.json中,然后我打开它来阅读它.我想知道是否可以直接读取它而不先存储到本地?
Esp*_*lma 22
您可以使用stdlib(json&urllib2)来避免使用外部命令:
import json
import urllib2
url = "http://httpbin.org/get"
response = urllib2.urlopen(url)
data = response.read()
values = json.loads(data)
Run Code Online (Sandbox Code Playgroud)
但我建议使用请求来简化代码.这里是来自文档的示例:
import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
r.status_code
200
r.headers['content-type']
'application/json; charset=utf8'
r.encoding
'utf-8'
r.text
u'{"type":"User"...'
r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
Run Code Online (Sandbox Code Playgroud)
HTH