在python中从URL获取数据太慢了

时间:2017-10-30 17:52:50

标签: python performance url

我从URL获取数据,但它恰好花了大约4到5秒,我发现它很长。这是我的代码:

import urlparse
import urllib2
import json
import sqlite3


class paribu:

#    def __init__(self):

    def getLatest(self):
        url = "https://www.paribu.com/ticker"
        #hdr is included due to an error in fetching data
        hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
           'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
           'Accept-Encoding': 'none',
           'Accept-Language': 'en-US,en;q=0.8',
           'Connection': 'keep-alive'}
        #ret = urllib2.urlopen(urllib2.Request(url,headers=hdr))
        ret = urllib2.urlopen(urllib2.Request(url,headers=hdr))
        #ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=returnTicker'))
        data = json.loads(ret.read())
        return data['BTC_TL']['last']

任何有助于加快速度的帮助表示赞赏!

0 个答案:

没有答案