我在从http://api.divesites.com提取JSON数组时遇到问题。 在python3.7中手动将数据设置为字符串时,可以看到数组。你知道吗
import json
def main():
hardcode = """{"request":{"str":null,"timestamp":1572865590,"loc":{"lat":"50.442","lng":"-4.08279999999999"},"mode":"sites","dist":"9","api":1},"sites":[{"currents":null,"distance":"7.47","hazards":null,"lat":"50.3378","name":"Fort Bovisand","water":null,"marinelife":null,"description":null,"maxdepth":null,"mindepth":null,"predive":null,"id":"24387","equipment":null,"lng":"-4.1285"},{"currents":null,"distance":"7.93","hazards":null,"lat":"50.3352","name":"Plymouth Breakwater","water":null,"marinelife":null,"description":null,"maxdepth":null,"mindepth":null,"predive":null,"id":"24388","equipment":null,"lng":"-4.1485"}],"version":1,"loc":{"lat":"50.442","lng":"-4.08279999999999"},"result":true}
"""
print("Original Data: ---", get_data(hardcode))
print("Site Data:----", get_data(hardcode)['sites'])
def get_data(data):
return json.loads(data)
if __name__=='__main__':
main()
输出
Original Data: --- {'request': {'str': None, 'timestamp': 1572865590, 'loc': {'lat': '50.442', 'lng': '-4.08279999999999'}, 'mode': 'sites', 'dist': '9', 'api': 1}, 'sites': [{'currents': None, 'distance': '7.47', 'hazards': None, 'lat': '50.3378', 'name': 'Fort Bovisand', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24387', 'equipment': None, 'lng': '-4.1285'}, {'currents': None, 'distance': '7.93', 'hazards': None, 'lat': '50.3352', 'name': 'Plymouth Breakwater', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24388', 'equipment': None, 'lng': '-4.1485'}], 'version': 1, 'loc': {'lat': '50.442', 'lng': '-4.08279999999999'}, 'result': True}
Site Data:---- [{'currents': None, 'distance': '7.47', 'hazards': None, 'lat': '50.3378', 'name': 'Fort Bovisand', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24387', 'equipment': None, 'lng': '-4.1285'}, {'currents': None, 'distance': '7.93', 'hazards': None, 'lat': '50.3352', 'name': 'Plymouth Breakwater', 'water': None, 'marinelife': None, 'description': None, 'maxdepth': None, 'mindepth': None, 'predive': None, 'id': '24388', 'equipment': None, 'lng': '-4.1485'}]
但是,当我尝试执行GET请求(使用urllib.request.urlopen()
)时,我只接收对象。你知道吗
import urllib.request
import json
def get_jsonparsed_data(url):
response = urllib.request.urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
def main():
url = 'http://api.divesites.com/?mode=sites&lat=-50.350874&lng=175.849890&dist=12'
print("Original Data: ---", get_jsonparsed_data(url))
print("Sites: ----", get_jsonparsed_data(url)['sites'])
if __name__=='__main__':
main()
输出
Original Data: --- {'request': {'str': None, 'timestamp': 1572866211, 'loc': {'lat': '-50.350874', 'lng': '175.849890'}, 'mode': 'sites', 'dist': '12', 'api': 1}, 'sites': [], 'version': 1, 'loc': {'lat': '-50.350874', 'lng': '175.849890'}, 'result': True}
Site Data:---- []
我是做错了什么,还是需要多走一步?你知道吗
解决了。我花了很长时间专注于调试代码,却忘了检查最后的API调用。结果我要么没有设置足够的距离,要么开始的经纬度错误。这与我硬编码的JSON不同。你知道吗
URL应该是:http://api.divesites.com/?mode=sites&lat=50.442&lng=-4.08279999999999&dist=9
愚蠢的错误。谢谢你的帮助。它让我看了看。你知道吗
首先,我将使用requests库,然后您可以找到解决方案here
相关问题 更多 >
编程相关推荐