In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.