有效url且无urllib2.SocketError,但在python中出现值错误(';ValueError:无法解码任何JSON对象';)
我试图为一些轨迹点调用地理转换API,url是有效的(用于测试),但得到了有效url且无urllib2.SocketError,但在python中出现值错误(';ValueError:无法解码任何JSON对象';),python,json,urllib2,asp.net-web-api,Python,Json,Urllib2,Asp.net Web Api,我试图为一些轨迹点调用地理转换API,url是有效的(用于测试),但得到了ValueError。此外,我不明白为什么它在几周前运行良好,但今天出现了一个错误 下面是错误消息和示例代码 如果我没有捕捉到ValueError 错误: ValueError Traceback (most recent call last) <ipython-input-37-49502611c9d0> in <module>()
ValueError
。此外,我不明白为什么它在几周前运行良好,但今天出现了一个错误
下面是错误消息和示例代码
如果我没有捕捉到ValueError
错误:
ValueError Traceback (most recent call last)
<ipython-input-37-49502611c9d0> in <module>()
5 ak = 'SWovHGOW7RV1kW4z1nbAsCAD'
6 result_1 = geo_decoding(test_coord_array_1,
----> 7 test_coord_array_1.shape[0], ak)
8 result_2 = geo_decoding(test_coord_array_2,
9 test_coord_array_2.shape[0], ak)
<ipython-input-36-a93995bd86c7> in geo_decoding(coord_array, n_coord_array, ak)
23 content = response.read()
24 print content
---> 25 result = json.loads(content)
26 response.close()
27 return result
/home/*/anaconda/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
336 parse_int is None and parse_float is None and
337 parse_constant is None and object_pairs_hook is None and not kw):
--> 338 return _default_decoder.decode(s)
339 if cls is None:
340 cls = JSONDecoder
/home/*/anaconda/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
364
365 """
--> 366 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
367 end = _w(s, end).end()
368 if end != len(s):
/home/*/anaconda/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
382 obj, end = self.scan_once(s, idx)
383 except StopIteration:
--> 384 raise ValueError("No JSON object could be decoded")
385 return obj, end
ValueError: No JSON object could be decoded
对于测试_2:
希望有人能为我指出问题所在,谢谢~好吧,像往常一样,提取一个显示这种行为的最小示例。您的json示例来自哪里?因为您的响应内容中没有这样的json。@UlrichEckhardt因为此API对地理坐标的数量有限制,比如不超过100,所以在第一次测试中,我复制了一对坐标25次。@Brunodesshuilliers数据是由出租车上配备的GPS设备收集的,但理论上,该API可以接受任何国家的任何有效地理坐标。我看到
response.read()
的输出有一个奇怪的字段expires
,但我不太确定这一点,因为我不了解html
语言,当我直接单击url时,它得到了正常的返回,如JsonLint
的输出所示。几周前一切正常,但今天……:(恐怕你误解了我的意思。我想建议你创建一个包含代码和数据的最小示例。你现在的问题很模糊。而且,如果你这样做,你自己可能会发现错误。
import numpy as np
import time
import urllib, urllib2, json
from httplib import BadStatusLine
def geo_decoding(coord_array, n_coord_array, ak):
trial = 0
host = "http://api.map.baidu.com/geoconv/v1/?"
if n_coord_array == 1:
coord_string = coord_array[0]
params = {'from': 1, 'to': 5, 'coords': coord_string, 'ak': ak}
else:
coord_string = ''
for coord in coord_array:
coord_string += coord + ';'
params = {'from': 1, 'to': 5, 'coords': coord_string[0:-1], 'ak': ak}
url = host + urllib.urlencode(params)
print url
while True:
try:
request = urllib2.Request(url)
response = urllib2.urlopen(request)
content = response.read()
print content
result = json.loads(content)
response.close()
return result
except urllib2.URLError as e:
trial += 1
if trial <= 2:
print 'e.reason.errno, try again...'
time.sleep(1)
else:
return None
except urllib2.socket.error:
trial += 1
if trial <= 2:
print 'SocketError, try again...'
time.sleep(1)
else:
return None
except ValueError:
trial += 1
if trial <= 2:
print 'ValueError, try again...'
time.sleep(1)
else:
return None
except BadStatusLine:
trial += 1
if trial <= 2:
print 'BadStatusLine, try again...'
time.sleep(1)
else:
return None
if __name__ == "__main__":
test_coord_array_1 = np.chararray((25,), itemsize = 256)
test_coord_array_1[:] = '116.404,39.915;116.414,39.975'
test_coord_array_2 = np.chararray((1,), itemsize = 256)
test_coord_array_2[:] = '116.404,39.915;116.414,39.975'
ak = 'SWovHGOW7RV1kW4z1nbAsCAD'
result_1 = geo_decoding(test_coord_array_1,
test_coord_array_1.shape[0], ak)
result_2 = geo_decoding(test_coord_array_2,
test_coord_array_2.shape[0], ak)
print type(result_1), type(result_2)
http://api.map.baidu.com/geoconv/v1/?to=5&ak=SWovHGOW7RV1kW4z1nbAsCAD&coords=116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975%3B116.404%2C39.915%3B116.414%2C39.975&from=1
<html><head><meta http-equiv="expires" content="0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><script language=javascript>location='http://10.108.255.249/muxjp.php?url='+document.location;</script></head></html>
ValueError, try again...
<html><head><meta http-equiv="expires" content="0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><script language=javascript>location='http://10.108.255.249/muxjp.php?url='+document.location;</script></head></html>
ValueError, try again...
<html><head><meta http-equiv="expires" content="0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><script language=javascript>location='http://10.108.255.249/muxjp.php?url='+document.location;</script></head></html>
http://api.map.baidu.com/geoconv/v1/?to=5&ak=SWovHGOW7RV1kW4z1nbAsCAD&coords=116.404%2C39.915%3B116.414%2C39.975&from=1
<html><head><meta http-equiv="expires" content="0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><script language=javascript>location='http://10.108.255.249/muxjp.php?url='+document.location;</script></head></html>
ValueError, try again...
<html><head><meta http-equiv="expires" content="0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><script language=javascript>location='http://10.108.255.249/muxjp.php?url='+document.location;</script></head></html>
ValueError, try again...
<html><head><meta http-equiv="expires" content="0"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="pragma" content="no-cache"><script language=javascript>location='http://10.108.255.249/muxjp.php?url='+document.location;</script></head></html>
<type 'NoneType'> <type 'NoneType'>
{
"status": 0,
"result": [
{
"x": 116.41662851562,
"y": 39.922701508455
},
{
"x": 116.42670828652,
"y": 39.982580969919
},
{
"x": 116.4166274269,
"y": 39.9227017898
},
{
"x": 116.42670474457,
"y": 39.98258699539
},
{
"x": 116.41663150895,
"y": 39.922701751779
},
{
"x": 116.426710471,
"y": 39.982584733879
},
{
"x": 116.41663504621,
"y": 39.922702532438
},
{
"x": 116.42670610694,
"y": 39.98258480313
},
{
"x": 116.41663259451,
"y": 39.92270635216
},
{
"x": 116.42670801779,
"y": 39.982587214783
},
{
"x": 116.41663368305,
"y": 39.922706342017
},
{
"x": 116.42671429148,
"y": 39.982587657882
},
{
"x": 116.41663177739,
"y": 39.922707444594
},
{
"x": 116.42670801551,
"y": 39.982583687523
},
{
"x": 116.41663858522,
"y": 39.922700601015
},
{
"x": 116.42670855839,
"y": 39.982579608952
},
{
"x": 116.41663531729,
"y": 39.922704157145
},
{
"x": 116.42670392386,
"y": 39.982583209786
},
{
"x": 116.41662878582,
"y": 39.922704489199
},
{
"x": 116.42670556213,
"y": 39.982585897096
},
{
"x": 116.41662824225,
"y": 39.92270340944
},
{
"x": 116.42670883412,
"y": 39.982584217194
},
{
"x": 116.41662905796,
"y": 39.922704486665
},
{
"x": 116.4267031084,
"y": 39.982587564011
},
{
"x": 116.41662932974,
"y": 39.922705026544
},
{
"x": 116.42670883237,
"y": 39.982581503918
},
{
"x": 116.41663341197,
"y": 39.92270471731
},
{
"x": 116.42670747176,
"y": 39.982586409456
},
{
"x": 116.41662878653,
"y": 39.922703404371
},
{
"x": 116.42671074165,
"y": 39.982581473619
},
{
"x": 116.41663558713,
"y": 39.922707680302
},
{
"x": 116.42671128593,
"y": 39.982579565669
},
{
"x": 116.41663205305,
"y": 39.922702017916
},
{
"x": 116.42670719533,
"y": 39.982580715903
},
{
"x": 116.41663341233,
"y": 39.922704174896
},
{
"x": 116.42670746808,
"y": 39.982580711575
},
{
"x": 116.41662769499,
"y": 39.92270802503
},
{
"x": 116.42670610344,
"y": 39.982579376577
},
{
"x": 116.41662851545,
"y": 39.922701779662
},
{
"x": 116.42670392456,
"y": 39.982584295097
},
{
"x": 116.41663695029,
"y": 39.922703870721
},
{
"x": 116.42670801534,
"y": 39.982583416196
},
{
"x": 116.41663259644,
"y": 39.922703368881
},
{
"x": 116.4267050175,
"y": 39.98258726239
},
{
"x": 116.41663830851,
"y": 39.922707654938
},
{
"x": 116.42670692345,
"y": 39.98258207687
},
{
"x": 116.41662823908,
"y": 39.922708291169
},
{
"x": 116.42671101633,
"y": 39.982584453895
},
{
"x": 116.41663477107,
"y": 39.922707145496
},
{
"x": 116.42670392456,
"y": 39.982584295097
}
]
}
{
"status": 0,
"result": [
{
"x": 116.41662769463,
"y": 39.922708567445
},
{
"x": 116.42670665244,
"y": 39.982584794474
}
]
}