我正在尝试以JSON格式从MapquestAPI获得响应。 使用相同的URL,我可以从bash调用时获得结果
wget -q -O - "http://www.mapquestapi.com/geocoding/v1/batch?key=F20%2C7a%3Do5-hrtl1&callback=renderBatch&json=%7B%27locations%27%3A+%5B%7B%27street%27%3A+%2711880+San+Pablo+Ave%2C+El+Cerrito%27%7D%2C+%7B%27street%27%3A+%271780+E+Buena+Vista+Dr%2C+Orlando%27%7D%2C+%7B%27street%27%3A+%2714+Greenway+Circle%2C+Sacramento%27%7D%2C+%7B%27street%27%3A+%27160+Kingsland+St%2C+Nutley%27%7D%2C+%7B%27street%27%3A+%27816+Western+Ave%2C+Glen+Ellyn%27%7D%5D%7D,options:{thumbMaps:false}}"
以python的方式进行:
import urllib2, urllib
import json
url = "http://www.mapquestapi.com/geocoding/v1/batch?key=Fmjtd%7Cluua2dua20%2C7a%3Do5-hrtl1&callback=renderBatch&json=%7B%27locations%27%3A+%5B%7B%27street%27%3A+%2711880+San+Pablo+Ave%2C+El+Cerrito%27%7D%2C+%7B%27street%27%3A+%271780+E+Buena+Vista+Dr%2C+Orlando%27%7D%2C+%7B%27street%27%3A+%2714+Greenway+Circle%2C+Sacramento%27%7D%2C+%7B%27street%27%3A+%27160+Kingsland+St%2C+Nutley%27%7D%2C+%7B%27street%27%3A+%27816+Western+Ave%2C+Glen+Ellyn%27%7D%5D%7D,options:{thumbMaps:false}}"
print url
data = json.dumps([1,2,3])
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read
print f.read
这是结果
<bound method _fileobject.read of <socket._fileobject object at 0x1a41bd0>>
任何建议如何在PY中获取JSON输出?