我对 python 非常陌生,并尝试解析看似 JSON 数据。
import requests
import json
URL = "http://myip/cgi-bin/lun/luci/;stok=/login?form=login"
payload = {
'operation': 'login',
'username': 'someuser',
'password': 'somepassword'
}
session = requests.session()
r = requests.post(URL, data=payload)
print (r.json())
我得到的返回是:
{u'data': {u'stok': u'0c2f16c954cd2cafccfaf1bf50000ac6'}, u'success': True}
我需要在单引号中使用字母/数字的随机集合0c2f16c954cd2cafccfaf1bf50000ac6
(它每次都会改变)。我在此回复中没有看到任何双引号。所以有几个问题:
- 我在响应中没有看到任何双引号。这还是json吗?
- 将随机字符的中间部分分配给变量以便我可以在接下来需要构建的请求网址中使用它的最佳方法是什么?
最佳答案
I dont see any double quotes in the response. Is this still json?
它是 JSON。 r.json()
将 JSON 响应反序列化为 Python dict
,然后 print
打印该 dict
。
What would be the best way to assign that middle part of random characters to a variable so I can use it in a request url I need to build next?
a_variable = r.json()["data"]["stok"]
关于Python - 有问题的 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53455460/