Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python获取json数据的最快、更好的方法_Python_Json - Fatal编程技术网

使用Python获取json数据的最快、更好的方法

使用Python获取json数据的最快、更好的方法,python,json,Python,Json,我使用的是json服务,通过以下方式获取数据: import urllib2 import json url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1" r = urllib2.urlopen(url) data = json.load(r) 我需要在第二

我使用的是json服务,通过以下方式获取数据:

import urllib2
import json
url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1"
r = urllib2.urlopen(url)
data = json.load(r)
我需要在第二个aprox前运行10-50个查询。最好的方法是什么

PD:没有json服务限制。
谢谢

在python方面,您没有什么可以做的

如果不是你的问题,你可能会考虑使用最新的SimJixon,它比标准库JSON的加载速度要快得多。请记住,当直接比较库时反序列化速度更快,当考虑整个请求/响应周期时,差异可能不值得。 要运行并行请求,您应该尝试:


显然,即使并行启动50个请求,您也会受到网络和远程服务器性能的限制。

只执行一个简单的for循环,结果并不理想。您显示的代码完全受网络性能的限制。除了并行执行网络通信之外,在Python方面你什么都做不了。这有什么不好的?您没有获得足够的请求量?正如@MartijnPieters所说,您当前的代码受网络性能的约束。要解决这个问题,您应该考虑使用线程或工作进程来并行生成请求。simplejson只是旧Python版本的外部维护版本。事实上,simplejson并没有停留在Python发行版发布的版本上。那么simplejson中神奇的加速从何而来?体系结构是一样的,Python stdlib具有相同的C扩展加速,我只在changelog中看到错误修复。不知道它们来自哪里,但它们存在。这个答案指出simplejson在加载时明显更快,而json在转储时更快。这些结果之间的距离不够大,不能称之为“重要的”。我认为一个量级的差异是显著的。我没有意识到,对于返回str作为某些输入,返回unicode作为其他输入,我的态度是傲慢的,这真的很可怕。
urls = ["http://nominatim.openstreetmap.org/reverse?format=json&lat=52.5487429714954&lon=-1.81602098644987&zoom=18&addressdetails=1",
        ....
       ]

requests = (grequests.get(u) for u in urls)

responses = grequests.map(requests)

for r in responses:
    print r.json()