【发布时间】:2026-02-23 06:35:01
【问题描述】:
我正在向英雄联盟 API 发送 JSON 文件请求。在其他 3 次尝试中,它对我有用,但最后却没有。我不知道为什么,也找不到任何错误。
我请求获取 JSON 文件
def challengerPlayers(region, types, APIkey):
URL = "https://" + region + ".api.pvp.net/api/lol/" + region + "/v2.5/league/master?type=" + types + "&api_key=" + APIkey
response = requests.get(URL)
return response.json()
我的网站功能用于返回结果。 地方,哪里出错,用注释突出显示。
@app.route('/hello', methods=['post'])
def hello():
region = request.form['region']
summonerName = request.form['summonerName']
APIkey = request.form['APIkey']
types = request.form['types']
responseJSON = getData(region, summonerName, APIkey)
ID = responseJSON[summonerName]['id']
ID = str(ID)
responseJSON2 = getRankedData(region, ID, APIkey)
divisionName = responseJSON2[ID][0]['name']
responseJSON3 = challengerPlayers(region, str(types), APIkey)
#Here is the problem ↓↓↓
challengerPlayers = responseJSON3['entries'][0]
#print challengerPlayers
return render_template('form_action.html', ID = ID, divisionName = divisionName, challengerPlayers = challengerPlayers)
最后但同样重要的是,我的网站表单
<form class="form" method="post" action="/hello">
<div class="form-group">
<label for="regio">Region</label>
<input type="text" name="region" />
</div>
<div class="form-group">
<label for="summonerNam">Summoner Name</label>
<input type="text" name="summonerName" />
</div>
<div class="form-group">
<label for="apiKe">API Key</label>
<input type="text" name="APIkey" />
</div>
<div class="form-group">
<label for="type">Ranked type</label>
<input type="text" name="types" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
PS:添加 JSON 文件
{
"queue": "RANKED_SOLO_5x5",
"name": "Nasus's Agents",
"entries": [
{
"leaguePoints": 0,
"isFreshBlood": false,
"isHotStreak": false,
"division": "I",
"isInactive": false,
"isVeteran": true,
"losses": 402,
"playerOrTeamName": "Ä L F A",
"playerOrTeamId": "28880245",
"wins": 445
}
}
【问题讨论】:
-
请提供例外情况
-
@ValentinLorentz 你的意思是错误吗?错误是:“POST /hello HTTP/1.1”500 -
-
是的,但这还不够。使用调试/测试模式显示完整错误。 flask.pocoo.org/docs/0.10/config/#builtin-configuration-values
-
我希望,你正在寻找这个:UnboundLocalError: local variable 'challengePlayers' referenced before assignment
-
好的。以后,请在问题正文中以文本形式提供错误。
标签: python json api flask python-requests