import json
import urllib
import re
import binascii
def asciirepl(match):
s = match.group()
return binascii.unhexlify(s[2:])
query = 'google'
p = urllib.urlopen('http://www.google.com/dictionary/json?callback=a&q='+query+'&sl=en&tl=en&restrict=pr,de&client=te')
page = p.read()[2:-10] #As its returned as a function call
#To replace hex characters with ascii characters
p = re.compile(r'\\x(\w{2})')
ascii_string = p.sub(asciirepl, page)
#Now decoding cleaned json response
data = json.loads(ascii_string)
運行它,我得到這個錯誤,ValueError異常解碼JSON
[email protected] /tmp $ python2 define.py
Traceback (most recent call last):
File "define.py", line 19, in <module>
data = json.loads(ascii_string)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 403 (char 403)
至於我想,JSON是沒有任何錯誤,因爲我從谷歌的服務器收到它。所有,我做的是刪除十六進制字符。任何幫助將不勝感激。
什麼是線1列403(及其周圍)? –
@TimPietzcker rathe而不是等待迴應,我從代碼隱含的URL下載了json文件,並自己對其進行了檢查。我懷疑Izkata做了類似的事情。 –
確實,我複製了shadyabhi的代碼並自己運行 – Izkata