Python 如何检查url是否包含JSON文件
我正在使用传奇联盟API,并试图从JSON文件中获取排名数据。但是,如果玩家不是30级,他就没有档案。 所以这里 它不会得到JSON文件,因为它不存在。如果URL不存在并且没有JSON文件,它将返回字符串 在这里,我将数据返回到HTML页面。但这也不管用Python 如何检查url是否包含JSON文件,python,json,api,flask,python-requests,Python,Json,Api,Flask,Python Requests,我正在使用传奇联盟API,并试图从JSON文件中获取排名数据。但是,如果玩家不是30级,他就没有档案。 所以这里 它不会得到JSON文件,因为它不存在。如果URL不存在并且没有JSON文件,它将返回字符串 在这里,我将数据返回到HTML页面。但这也不管用 region = request.form['region'] summonerName = request.form['summonerName'] APIkey = "45afde27-b628-473f-9a94-feec8eb86
region = request.form['region']
summonerName = request.form['summonerName']
APIkey = "45afde27-b628-473f-9a94-feec8eb86094"
types = request.form['types']
responseJSON = getData(region, summonerName, APIkey)
ID = responseJSON[summonerName]['id']
ID = str(ID)
responseJSON2 = getRankedData(region, ID, APIkey)
if not responseJSON2:
divisionName = "Unranked"
else:
divisionName = responseJSON2[ID][0]['name']
responseJSON3 = getChallengerPlayers(region, str(types), APIkey)
challengerPlayers = responseJSON3['entries'][0]['wins']
#print challengerPlayers
return render_template('form_action.html', ID = ID, divisionName = divisionName, challengerPlayers = challengerPlayers)
我得到了这个错误:
Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Hanisek\Documents\Visual Studio 2015\Projects\FlaskWebProject2\FlaskWebProject2\FlaskWebProject2\views.py", line 53, in hello
responseJSON2 = getRankedData(region, ID, APIkey)
File "C:\Users\Hanisek\Documents\Visual Studio 2015\Projects\FlaskWebProject2\FlaskWebProject2\FlaskWebProject2\views.py", line 21, in getRankedData
Open an interactive python shell in this framereturn response.json()
File "C:\Python27\lib\requests\models.py", line 805, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
通过在浏览器中运行api url来检查api url返回的内容始终是一个好主意。我猜它会返回404错误,因为信息不存在 在这种情况下,我建议在继续JSON解析之前检查是否存在404错误 请求有一个名为的函数,如果存在404错误,它将返回404错误。 示例代码:
r = request.get("API STRING")
if r.status_code != 404:
r.json()
不太了解LOL,但是有没有理由让你的程序使用if/then语句来检查播放器的级别,然后只检查json文件,如果播放器的级别足够高,可以有一个呢?你可以尝试检查URL是否存在,json格式是否正确,或者页面是否抛出了40X状态码
try:
r = requests.get("URL")
assert r.status_code < 400
return r.json()
except (ValueError, ConnectionError, AssertionError):
return ''
试试看:
r=请求。获取(“URL”)
断言r.status\u代码<400
返回r.json()
除了(ValueError、ConnectionError、AssertionError):
返回“”
您能提供响应的一个片段吗?@albert您的意思是什么?由于响应无法解码为json对象,因此引发了错误。因此,看看回复可能会有所帮助。非常感谢您的回答。比100行编码更好。没问题,通常最简单的答案是正确的~occams razor
try:
r = requests.get("URL")
assert r.status_code < 400
return r.json()
except (ValueError, ConnectionError, AssertionError):
return ''