[英]Can't fetch data from python request response
I'm new to Python, I'm using "requests" to send a post request in api endpoint and the response I get like this:我是 Python 的新手,我使用“请求”在 api 端点发送一个 post 请求,我得到的响应是这样的:
{"success":true,"data":4503615,"message":"Thanks","lid":""}
and my code like this :我的代码是这样的:
import requests
para1 = {'username': 'xxxx' ,'password': 'xxx'}
req1 = requests.post('https://testpy.com/login', data = para1)
print(req1)
the response I got is similar to dictionary type except the "success":true , I just want that "data":4503615 numbers in a new variable.我得到的响应类似于字典类型,除了"success":true ,我只想要一个新变量中的"data":4503615数字。 I tried to fetch that like this我试着这样取
import requests
para1 = {'username': 'xxxx' ,'password': 'xxx'}
req1 = requests.post('https://testpy.com/login', data = para1)
qq = req1.text
cc = qq["data"]
print(cc)
But it didn't show the number, Is there any way to get that number?但是没有显示号码,有什么办法可以得到那个号码吗? Thanks谢谢
Use req1.json()
instead of req1.text
, it parses the text of the response to a dictionary you can use in the way you describe above.使用req1.json()
而不是req1.text
,它将响应文本解析为您可以按照上述方式使用的字典。
So replace所以更换
qq = req1.text
with和
qq = req1.json()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.