1

私は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
4

1 に答える 1

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()
于 2013-06-22T17:25:17.527 に答える