我试图使用AJAX在JavaScript中调用python脚本,但是,我得到的响应是原始代码。这个想法是从python脚本中将数据写入JSON文件,但是它不能正确地运行脚本并覆盖文件中已有的数据。为什么python在这种情况下不起作用?
Screenshot of the site currently
PYTHON:
import requests
import json
request_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
'Host': 'stats.nba.com',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}
team = '1610612742'
url = "https://stats.nba.com/stats/commonteamroster/?Season=1999-00&TeamID=" + team
response = requests.get(url, headers = request_headers)
data = response.json()
print (response)
class Player():
def __init__(self, name, position, height, weight):
self.name = name
self.position = position
self.height = height
self.weight = weight
objs = []
def run():
x = 0;
players = []
for i in data['resultSets'][0]['rowSet']:
name = data['resultSets'][0]['rowSet'][x][3]
position = data['resultSets'][0]['rowSet'][x][5]
height = data['resultSets'][0]['rowSet'][x][6]
weight = data['resultSets'][0]['rowSet'][x][7]
stats = 'Name: ' + name + ' Position: ' + position + ' Height: ' + height + ' Weight: ' + weight
objs.append(Player(name, position, height, weight))
x+=1
return (players)
val = 0
list = run()
data2 = {}
for x in objs:
data2[val] = x.name + ", " + x.position + ", " + x.height + ", " +x.weight;
val+=1
json_data = json.dumps(data2)
with open('data.json', 'w') as outfile:
json.dump(json_data + "test", outfile)
print (json_data)
HTML:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="data.json"></script>
<meta charset="utf-8">
<title></title>
</head>
<body>
<select id="s1">
<option value="1610612742">Mavericks</option>
<option value="1610612756">Suns</option>
<option value="1610612744">Warriors</option>
<option value="1610612739">Cavaliers</option>
</select>
<select id="s2">
<option value="1999-00">1999-00</option>
<option value="2000-01">2000-01</option>
<option value="2001-02">2001-02</option>
<option value="2002-03">2002-03</option>
</select>
<button id="button" onclick="run()"> Send </button>
<div id="mine">
</div>
</body>
<script type="text/javascript">
var json = '';
var output = "";
window.onload = function() {
$.getJSON( "data.json", function(response) {
json = $.parseJSON (response);
var count = Object.keys(json).length;
for (x = 0; x <= count-1; x++) {
output = output + json[x] + "\n";
console.log(json[x]);
}
document.getElementById("mine").innerText = output;
});
}
function run () {
var e = document.getElementById("s1");
var strTeam = e.options[e.selectedIndex].value;
var e = document.getElementById("s2");
var strYear = e.options[e.selectedIndex].value;
var comp = {'Team':[strTeam], 'Year':[strYear]}
console.log(comp);
$.ajax({
type: 'GET',
data: comp,
url: 'script.py',
success: function(response){
alert(response);
console.log(response);
}
});
}
</script>
</html>
JSON:
"{\"0\": \"Michael Finley, G, 6-7, 215\", \"1\": \"Damon Jones, G, 6-3, 185\", \"2\": \"Steve Nash, G, 6-3, 195\", \"3\": \"Robert Pack, G, 6-2, 190\", \"4\": \"Erick Strickland, G, 6-3, 210\", \"5\": \"Greg Buckner, G, 6-4, 210\", \"6\": \"Cedric Ceballos, F, 6-7, 220\", \"7\": \"Hubert Davis, G, 6-5, 183\", \"8\": \"Gary Trent, F, 6-8, 250\", \"9\": \"Dirk Nowitzki, F-C, 7-0, 237\", \"10\": \"Shawn Bradley, C, 7-6, 263\", \"11\": \"Sean Rooks, C, 6-10, 270\"}"