2013-06-22 3 views
1

저는 파이썬과 코딩에 익숙하지 않습니다. JSON을 읽고 인쇄 할 때 12 초 (너무 깁니다) 걸립니다. URL을로드하여 더 빨리 읽고 인쇄하는 다른 방법이 있습니까?Python이 url에서 더 빨리 읽고 simplejson을로드 중입니까?

배열? 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 

답변

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() 
+0

가 정말 감사합니다 !!!!!!!!!! 그것은 나를 많이 돕는다! =) –

관련 문제