简体   繁体   中英

Fetching data from webpage using python and downloading output it to a file

I am trying to fetch some data from the webpage ( http://www.usgs.gov/ ) using python and JSON. And it works fine when i executed this script (i found this script in one tutorial), but when i am trying to get this output in to a local file, i am getting some errors in the last two lines,saying that invalid syntax(":" ) and got errors as well when i insert f.close() I googled it and changed some script but it doesn't work. Need help to fix this. I am using Python IDLE version 2.7.5.

    import urllib2
    import json

    #Example file to parse and process JSON
    f = open("output.txt")

    #use the JSON module to load the string into a dictionary
    def printResults(data):
        theJSON = json.loads(data)

    #access the contents of JSON like any othe object
        if "title" in theJSON["metadata"]:
            f.write( theJSON["metadata"]["title"])

    #output the no of events + magnitude and each event name
        count = theJSON["metadata"]["count"];
        f.write( str(count) + " events recorded")

    def main():
        urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.geojson"

    #open the url and read the data
       webUrl = urllib2.urlopen(urlData)
       print webUrl.getcode()
       if (webUrl.getcode() == 200):
          data = webUrl.read()
          printResults(data)
       else:
          f.write( "Received an error from server,can't retrieve results" + str(webUrl.getcode())

    if __name__=="__main__":
       main()

You're missing a closing brace on this line:

 f.write( "Received an error from server,can't retrieve results" + str(webUrl.getcode())

Also, your indentation is not consistent. You need to make sure your indents are always exactly four spaces. It's probably best to use a editor that automatically does this for you.

got errors as well when i insert f.close()

Although it's always a good practice, you don't need to close files explicitly in python. They will be closed when they are garbage collected (typically, after there are no references to the object, such as when the program terminates, in this case)

I made 2 changes in this script, and i think the scripts is working efficiently. The first, I add the second argument to the open function which is 'w' for write,

**f = open("output.txt",'w')**

and the second is in the latest lines of the file as follow :

if (webUrl.getcode() == 200):
    data = webUrl.read()
    printResults(data)
else:
    f.write( "Received an error from server,can't retrieve results" + str(webUrl.getcode()))

Be careful with indentation, and don't forget parentheses !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM