Python gets interface data, parses JSON, writes files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.