Python gets interface data, parses JSON, writes files
For the example of practiced hand, get the JSON data from the National Weather Interface, write it to the file, and parse the JSON;
In general, Python code is much less than Java in terms of code volume. And Python looks more intuitive;
Here's the code:
Import typesimport Urllib2import Jsonduan = "--------------------------" #在控制台断行区别的 # Get Network data with Urllib2 def registerurl (): Try:url = "http://m.weather.com.cn/data/101010100.html" data = Urllib2.urlopen (URL). Read () return dataexcept Exception , E:print e# Write file def jsonfile (fileData): Files = open ("D:\json.txt", "W") File.write (FileData) file.close () # Parsing JSON data obtained from the network Def praserjsonfile (jsondata): value = Json.loads (jsondata) rootlist = Value.keys () print Rootlistprint Duanfor Rootkey in rootlist:print rootkeyprint duansubvalue = value[rootkey]print subvalueprint duanfor subkey in Subvalue :p rint subkey,subvalue[subkey]if __name__ = = "__main__": # XInput = Raw_input () # x = 130# Xvalue = CMP (x,xinput) # print Xva lue# Print X/100.0data = Registerurl () # jsonfile (data) praserjsonfile (data)
Python gets interface data, parses JSON, writes files