程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python processes JSON data returned by request

編輯:Python

One . Use Python Of requests Library request —— Response result processing

In practice , The response of many interfaces is json Formatted data , It needs to be processed and analyzed . involves json There are two methods of data processing : Serialization and deserialization :

python in serialize —— take python The dictionary of is converted into json Format string , For storage or transmission ;

Deserialization —— take json Format string to python Dictionaries , It is used for analyzing and processing .

JSON and DICT Format conversion method :

import json
# Serialized into json character string
d = {‘name':‘jod'}
j = json.dumps(d)
# Deserialize into a dictionary
print json.loads(j)

And in the requests In the library , no need json.loads Method to deserialize , It provides Of the response object json Method , Used to correct json Deserialize the response body of :

r = requests.get(url)

r.json()

Two . request The returned data can be processed in two ways :

    1. Yes request The response object returned from the initiation request is .json() operation ,.json The operation returns a dictionary type ( If the returned result is empty, an error will be reported ), as follows :

    2. Yes request In the response object of the request text Data processing json.loads(text) operation , The dictionary type is returned after the operation ( This method is recommended , Judge before use text Is it empty , An error will also be reported if it is empty ), as follows :

3、 ... and .  Use request Send the request and process the returned result -- Basic template

import requests
import json
# With get Method to request , You can attach parameters directly to the back , You can also pass in parameters to
response=requests.get(f"http://XXXXX/anyq?question={question}")
param={"question":question}
res = requests.get(url='https://b.faloo.com/l/0/1.html',params=param)
# The formal parameter here is params
# If it is post, We can use data Form as a parameter
params = {"username": self.username,"email": self.email, "password": self.password}
response = requests.post(request_url, data=params)
# The formal parameter here is data
# If the data returned is unicode code , for example
"""
"answer\":\"\u8bb2\u8bdd\u4ea4\u6d41\u6700\u5c11\u76f8\u96941\u7c73\uff0c\u6700\u597d2\u7c73\u3002
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u98de\u6cab\u4f20\u64ad\u53ea\u6709\u4e0e\u4f20\u67d3\u6
e90\u8fd1\u8ddd\u79bb\u63a5\u89e6\u65f6\u624d\u53ef\u80fd\u5b9e\u73b0\u3002\u98de\u6cab
"""
# Reference resources https://www.cnblogs.com/573734817pc/p/10855147.html decode
decode_rs=response.text.encode("utf-8").decode('unicode_escape')
# If the json Format return , Use it directly json analysis , instead of text
json_rs=response.json()
print(json_rs)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved