我是python和编码的新手。当我正在阅读JSON并打印它时,需要12秒(这太长了)。是否还有其他方法可以加载URL并更快地读取和打印它?
阵列? for
循环?如果您需要API密钥进行测试,可以从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
答案 0 :(得分:1)
可能瓶颈是多个GET请求。通过使用httplib
库可以实现显着的加速,这可以让您更好地控制底层连接。
试试这个:
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()