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 回答