python - 如何解析来自 Python 请求的 JSON 响应?

标签 python python-requests

我正在尝试解析使用 Python 请求库发出 请求 时得到的 response.text。例如:

def check_user(self):
    method = 'POST'
    url = 'http://localhost:5000/login'
    ck = cookielib.CookieJar()
    self.response = requests.request(method,url,data='username=test1&passwd=pass1', cookies=ck)
    print self.response.text

当我执行这个方法时,输出是:

{"request":"POST /login","result":"success"}

我想检查 "result" 是否等于 "success",忽略前面的内容。

最佳答案

manual建议:if self.response.status_code == requests.codes.ok:

如果这不起作用:

if json.loads(self.response.text)['result'] == 'success':
   whatever()

关于python - 如何解析来自 Python 请求的 JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106702/

相关文章:

python - 无法通过 tor ConnectionRefusedError : [WinError 10061] 发出 python 请求

python - 应用程序用户记录的地理位置 - 哪个位置最重要?

python - 在 python 中获取 os.system 的输出并在之后处理它

python - 如何使用 Python 的请求模块将 "log in"发送到网站?

python - 我怎样才能刮掉所有击球手的名字?

python - 请求正文具有无效的 json 格式 - Python

python - 颜色感知图像哈希

python - 如何使用数据框中的值更好地构造 SQL 语句?

python - 我想打印列表中从 1 个索引开始的一对元素

python - 如何使用Python将嵌套字典/json插入MySQL表