Saturday 15 January 2011

json - Python reading faster on url and loading simplejson? -


I'm new to python and coding. When I'm reading JSON and printing it, it takes 12 seconds (which is too long). Are there any other ways to load a URL and read it and print it faster?

Array? for loops? If you need an API to test, you can get them.

  Import xml.dom.minidom, xml.sax.saxutils Import import from imported socket import timed out import datetime import time import simplejson ': //api.forecast.io/forecast/ apikey / 1.37871,103.848808 https' resultnyp json import urllib2 import sys, os, platform, then import Sched, urllib2 import urlopen import again urlnyp = the xml.dom contentnyp = urllib2.urlopen (urlnyp) imports as time minidom = Resultneepkreed () Urltmins = 'https: //apiaikforkastkio/forkast/apake/lk353092,l03k945229' Resulttmins = Urllib2kurlopen (Urltmins) Kantenttmins = Resulttminskreed () Urlkk = 'https: // API. Forecast.io/forecast/apikey/1.3975669,103.7473389 'resultcck = urllib2.urlopen (urlcck) contentcck = resultcck.read () urlyishun =' https: // a Pikforkastkio/forkast /apake/lk429463,l03k835l82 'Resultyishun = Urllib2kurlopen (Urlyishun) Kantentyishun = Resultyishunkreed () Urlredhil =' https: //apiaikforkastkio/forkast/apake/lk289732,l03k8l67 5 "Resultredhil = Urllib2kurlopen (Urlredhil) Kantentredhil = Resultredhilkreed () Veatherfaoreshastnyp = Jasonklods (Kantentoneep) Veatherfaorecastkkk = Jasonklods (Contentsk) Veatherfaoreksattmins = Jasonklods (Kantenttmins) Veatherfaorekastredil = Jasonklods (Kantentredhil) Veatherfaorecastyishun = ['Current']] for key in Json.loads (contentyishun) currentnyp = sorted (currentnyp): print '{0}: {1}'. Format (key, currently [key]) print 'psiAverage:' + str ( 'Psi_avg') print "Latitude: '+ str (Musmarekasteep [' Latitude ']) print" Longitude:' + str (Musmanaksainap [ 'longitude' ]) Print 'Location: Ang-Mo-Cao' Print Current Region = Weatherworld [currently "Key for" (currently): Print '{0}: {1}'. Format (key, Wartmankshik [key]) print 'psiAverage:' + str (psi_avg) print 'Latitude:' + str (weatherforecastcck [ 'Latitude']) print "Longitude: '+ str (Musmarecasisikak [' longitude ']) Print 'Location: Cho-Chu-Kang' Print Current areas = weatherF print 'psiAavverage:' + str (psi_avg) Print 'Latitude:' + 'To print' is currently sorted for [currently] Key (currently ticket ): Print '{0}: {1}'. Print ('key', currently printed '[key]' (latitude ')) Print' Longitude: '+ str (WeatherWorldTempins [' Longitude ']) Print' Location: Tempins' print currently = Weather categories [currently] Sorted for key (Currently): Print '{0}: {1}'. Print 'longitude:' + str ('at present): print' psiAverage: '+ str (psi_avg) print' Latitude: '+ str (weatherforecastyishun [' latitude ']) Print' {0}: {1} ' Format (Main, Current Call [Key]] 'Print' Location: Jesson has been sorted for 'Print Friedel = Weatherworthfeld [' Current ']. Print 'PSIAverage:' + str (psi_avg) Print 'Latitude:' + str (Weather Forecastly ['Latitude']) Print 'Longitude:' + str (Weatherworldld ['Longitude']) Print 'Location: Redhill' Print  

Maybe there is more than one GET request descending. You may possibly httplib Using a library, you can get a momentary speed that gives you more control over the underlying connection.

Try this:

  import httplib host = 'api.forecast.io' cone = httplib.HTTPSConnection (host) urlnyp = '/forecast/apikey/1.37871, 103.848808 'Conn.request (' gET ', urlnyp) resultnyp = conn.getresponse () contentnyp = resultnyp.read () urltampines =' /forecast/apikey/1.353092,103.945229 'conn.request (' GET ', urltampines) As a result = Conn.getresponse () contenttampines = resultstampines.read () # ... conn.close ()    

No comments:

Post a Comment