質問

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

Arrays? for loops? If you need API keys to test, You can get them from Forecast for Developers.

import xml.dom.minidom, xml.sax.saxutils
import logging
import httplib
from socket import timeout
import datetime
import time
import simplejson as json
import urllib2
import sys, os, platform, re
import sched, time
from xml.dom import minidom
from urllib2 import urlopen
import re

urlnyp='https://api.forecast.io/forecast/apikey/1.37871,103.848808'
resultnyp = urllib2.urlopen(urlnyp)
contentnyp = resultnyp.read()

urltampines='https://api.forecast.io/forecast/apikey/1.353092,103.945229'
resulttampines = urllib2.urlopen(urltampines)
contenttampines = resulttampines.read()

urlcck='https://api.forecast.io/forecast/apikey/1.3975669,103.7473389'
resultcck = urllib2.urlopen(urlcck)
contentcck = resultcck.read()

urlyishun='https://api.forecast.io/forecast/apikey/1.429463,103.835182'
resultyishun = urllib2.urlopen(urlyishun)
contentyishun = resultyishun.read()

urlredhill='https://api.forecast.io/forecast/apikey/1.289732,103.81675'
resultredhill = urllib2.urlopen(urlredhill)
contentredhill = resultredhill.read()


weatherForecastnyp = json.loads(contentnyp)
weatherForecastcck = json.loads(contentcck)
weatherForecasttampines = json.loads(contenttampines)
weatherForecastredhill = json.loads(contentredhill)
weatherForecastyishun = json.loads(contentyishun) 



currentlynyp = weatherForecastnyp['currently']
for key in sorted(currentlynyp):
    print '{0} : {1}'.format(key, currentlynyp[key])
print 'psiAverage : ' + str(psi_avg)
print 'latitude : ' + str(weatherForecastnyp['latitude'])
print 'longitude : ' + str(weatherForecastnyp['longitude'])
print 'location : Ang-Mo-Kio'
print


currentlycck = weatherForecastcck['currently']
for key in sorted(currentlycck):
    print '{0} : {1}'.format(key, currentlycck[key])
print 'psiAverage : ' + str(psi_avg)
print 'latitude : ' + str(weatherForecastcck['latitude'])
print 'longitude : ' + str(weatherForecastcck['longitude'])
print 'location : Choa-Chu-Kang'
print


currentlytampines = weatherForecasttampines['currently']
for key in sorted(currentlytampines):
    print '{0} : {1}'.format(key, currentlytampines[key])
print 'psiAverage : ' + str(psi_avg)
print 'latitude : ' + str(weatherForecasttampines['latitude'])
print 'longitude : ' + str(weatherForecasttampines['longitude'])
print 'location : Tampines'
print

currentlyyishun = weatherForecastyishun['currently']
for key in sorted(currentlyyishun):
    print '{0} : {1}'.format(key, currentlyyishun[key])
print 'psiAverage : ' + str(psi_avg)
print 'latitude : ' + str(weatherForecastyishun['latitude'])
print 'longitude : ' + str(weatherForecastyishun['longitude'])
print 'location : Yishun'
print


currentlyredhill = weatherForecastredhill['currently']
for key in sorted(currentlyredhill):
    print '{0} : {1}'.format(key, currentlyredhill[key])
print 'psiAverage : ' + str(psi_avg)
print 'latitude : ' + str(weatherForecastredhill['latitude'])
print 'longitude : ' + str(weatherForecastredhill['longitude'])
print 'location : Redhill'
print
役に立ちましたか?

解決

Probably the bottleneck is the multiple GET requests. You can probably achieve a significant speed-up by using the httplib library which gives you more control over the underlying connection.

Try this:

import httplib

host = 'api.forecast.io'
conn = 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)
resulttampines = conn.getresponse()
contenttampines = resulttampines.read()

# ...

conn.close()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top