Get the App
SLTechnology News&Howtos  ›  Development  › 

How to fully parse the data returned by the interface in python

Shulou Source: shulou.com Published: 2022-06-01 18:18:32 09月14日 Update

This article introduces the relevant knowledge of "how to fully parse the interface return data from python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Parsing interface returns data 1. Data in json format

Convert to a single {key,value} form and store each dict in list

Def parse (self,data): # parse the data in json format to generate list for key, value in data.items (): if isinstance (value, dict): self.parse (value) elif isinstance (value, list): for k in range (len (value)): # because list does not have items, it traverses the values in lis one by one Then parse self.parse (value [k]) else: self.L.append ({key: value}) return self.L2 for each item of list, and compare request and expect one by one into list.

Know whether the expected value and the return value match

Def compare (self,resjson,targjson): # compare two numeric returns, return True if equal, otherwise return false self.init () res_list = self.parse (resjson) self.init () targ_list = self.parse (targjson) count = len (targ_list) while count: if targ_ list [count-1] in res_list: # list, member operator In or not in count-= 1 else: return False return True3, test to see if it is correct if _ _ name__ = = "_ main__": t = Tools () request = {"head": {"clientId": "12", "appUDID": "," appVersion ":" "channelId": "," innerMedia ":", "outerMedia": "," subClientId ":" H6 "," origin ":"," test ": [{" h ":" "}]}," body ": {" userName ":" your user name " "password": "password"} expect = {"username": "your username", "password": "password"} # print (t.parse (request)) print (t.compare (request,expect))

Result: True or False is returned, and False is returned because the case of both sides of the username is not the same.

Summary: this parse plus compare function, you can directly assert () when the API returns the result, and get the true or false

Complete code class Tools: def _ _ init__ (self): self.L = [] def init (self): self.L = [] def parse (self,data): # parsing data in json format Generate list for key, value in data.items (): if isinstance (value, dict): self.parse (value) elif isinstance (value, list): for k in range (len (value)): # because list does not have items, it traverses the values in lis one by one Then parse each item of list self.parse (value [k]) else: self.L.append ({key: value}) return self.L def compare (self,resjson,targjson): # compare the two values returned if they are equal Return True, otherwise return false self.init () res_list = self.parse (resjson) self.init () targ_list = self.parse (targjson) count = len (targ_list) while count: if targ_ list [count-1] in res_list: # list, member operator In or not in count-= 1 else: return False return Truepython request API The data code returned by the crawl is as follows: # introduction packet import urllib2import jsonimport sys url = 'http://alisleepy.top' # interface address responseResult = urllib2.urlopen (url, None, timeout=2000) code = responseResult.getcode () print' interface status code:'+ str (code) if code! = 200: sys.exit ('request failed,please check interface is contacts') # check the data to see if it is in line with the expected data = json.loads (responseResult.read ()) # deserialize json data print 'code:' + str (data ['code]) print' msg message:'+ str (data ['msg']) "how to fully parse the data returned by the API". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Data interface format two code content member numerical value more knowledge result operator generation operation utility consistency learning and then information function. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Xiaomi Docker NVidia