响应数据.json()与.text区别
- json():返回类型为字典,可以通过键名来获取响应的值
- text:返回的类型为字符串,无法通过键名来获取响应的值
- 提示:共同点长得都像字典
import requests
import json
url="https://10.65.*.*/api/*/*/auth/login/"
headers={
"Content-Type":"application/json"
}
data={"login_name": "***","password": "***"}
r=requests.post(url,data=json.dumps(data),headers=headers,verify=False)
r_json = r.json()
r_text = r.text
print("r_json:" , r_json)
print("r_json类型为:" , type(r_json))
print("r_json通过键名获取值:" , r_json['result'])
print("r_text:" , r_text)
print("r_text类型为:" , type(r_text))
print("r_text 通过键名获取值:" , r_text['result'])
print(r.status_code)
D:\python3.6\python.exe F:/python_study/study/test03_拓展(响应对象.text与响应对象.json()的区别).py
D:\python3.6\lib\site-packages\urllib3\connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host '10.65.66.211'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html
InsecureRequestWarning,
Traceback (most recent call last):
File "F:/python_study/study/test03_拓展(响应对象.text与响应对象.json()的区别).py", line 50, in <module>
print("r_text 通过键名获取值:" , r_text['result'])
TypeError: string indices must be integers
r_json: {'status': 2000, 'message': 'need update', 'result': {'app_name': '运维门户', 'binded': [], 'customer_status': True, 'last_ip': '10.65.66.102', 'app_id': 'tembin', 'create_time': '1601022572', 'partner': None, 'account_type': 'user', 'contact_name': 'tenant_2h', 'partner_id': None, 'account_name': 'tenant_2h', 'customer_name': '666_tx', 'original_ids': [], 'user_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'parent_id': None, '_login_name': 'tenant_2h', 'op_user_id': '', 'last_login': 1606894195, 'formal': True, 'login_info_enable': 0, 'enable': True, 'first_time': 0, 'account_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'account_source': 'pamc', 'account_role': 'customer_user', 'avatar_uploaded': False, 'login_source': 'web', 'desc': '{"last_ip": "10.65.66.102", "last_login": 1606894195}', 'remark': '', 'customer_id': '995c1cdd-643a-316e-b1e9-263fffd59b24', 'contact_type': 'customer', 'login_info': {}, 'need_update': True, 'session_id': '2vyeiv4rpbvz0hftpiz70mnw3zh8xxnq', 'login_name': 'tenant_2h', 'customer_type': 8, 'yichuang_id': None, 'need_verify_code': 0, 'operation_mode': 0}, 'module': 'krosa'}
r_json类型为: <class 'dict'>
r_json通过键名获取值: {'app_name': '运维门户', 'binded': [], 'customer_status': True, 'last_ip': '10.65.66.102', 'app_id': 'tembin', 'create_time': '1601022572', 'partner': None, 'account_type': 'user', 'contact_name': 'tenant_2h', 'partner_id': None, 'account_name': 'tenant_2h', 'customer_name': '666_tx', 'original_ids': [], 'user_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'parent_id': None, '_login_name': 'tenant_2h', 'op_user_id': '', 'last_login': 1606894195, 'formal': True, 'login_info_enable': 0, 'enable': True, 'first_time': 0, 'account_id': 'af8e4d59-64e0-3cde-973e-3277d2cdc955', 'account_source': 'pamc', 'account_role': 'customer_user', 'avatar_uploaded': False, 'login_source': 'web', 'desc': '{"last_ip": "10.65.66.102", "last_login": 1606894195}', 'remark': '', 'customer_id': '995c1cdd-643a-316e-b1e9-263fffd59b24', 'contact_type': 'customer', 'login_info': {}, 'need_update': True, 'session_id': '2vyeiv4rpbvz0hftpiz70mnw3zh8xxnq', 'login_name': 'tenant_2h', 'customer_type': 8, 'yichuang_id': None, 'need_verify_code': 0, 'operation_mode': 0}
r_text: {"status": 2000, "message": "need update", "result": {"app_name": "\u8fd0\u7ef4\u95e8\u6237", "binded": [], "customer_status": true, "last_ip": "10.65.66.102", "app_id": "tembin", "create_time": "1601022572", "partner": null, "account_type": "user", "contact_name": "tenant_2h", "partner_id": null, "account_name": "tenant_2h", "customer_name": "666_tx", "original_ids": [], "user_id": "af8e4d59-64e0-3cde-973e-3277d2cdc955", "parent_id": null, "_login_name": "tenant_2h", "op_user_id": "", "last_login": 1606894195, "formal": true, "login_info_enable": 0, "enable": true, "first_time": 0, "account_id": "af8e4d59-64e0-3cde-973e-3277d2cdc955", "account_source": "pamc", "account_role": "customer_user", "avatar_uploaded": false, "login_source": "web", "desc": "{\"last_ip\": \"10.65.66.102\", \"last_login\": 1606894195}", "remark": "", "customer_id": "995c1cdd-643a-316e-b1e9-263fffd59b24", "contact_type": "customer", "login_info": {}, "need_update": true, "session_id": "2vyeiv4rpbvz0hftpiz70mnw3zh8xxnq", "login_name": "tenant_2h", "customer_type": 8, "yichuang_id": null, "need_verify_code": 0, "operation_mode": 0}, "module": "krosa"}
r_text类型为: <class 'str'>
Process finished with exit code 1