Python json响应

2024-10-04 09:23:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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中的随机字母/数字集合(每次都会更改)。我没有看到任何双引号在这个答复。有几个问题:

  1. 我在回复中没有看到任何双引号。这还是json吗
  2. 将随机字符的中间部分赋给变量以便在下一步需要构建的请求url中使用它的最佳方法是什么

Tags: 数据importjsonhttpurldatasessionlogin
1条回答
网友
1楼 · 发布于 2024-10-04 09:23:54

I dont see any double quotes in the response. Is this still json?

它是JSONr.json()反序列化了对Python dict的JSON响应,然后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"]

相关问题 更多 >