Python jsonResponse=r.json()name错误:name';r';没有定义

Python jsonResponse=r.json()name错误:name';r';没有定义,python,python-3.x,Python,Python 3.x,我试图从EPL英超梦幻足球官方网站上获取一些数据,但遇到了jsonResponse的问题 已尝试安装simplejsonpip安装simplejson 不确定还要尝试什么,请参阅我的代码: import pandas as pd import json import requests from pandas.io.json import json_normalize # Define a function to get info from the FPL API and save to the

我试图从EPL英超梦幻足球官方网站上获取一些数据,但遇到了
jsonResponse
的问题

已尝试安装simplejson
pip安装simplejson

不确定还要尝试什么,请参阅我的代码:

import pandas as pd
import json
import requests
from pandas.io.json import json_normalize

# Define a function to get info from the FPL API and save to the specified file_path
# It might be a good idea to navigate to the link in a browser to get an idea of what the data looks like

def get_json(file_path): r = requests.get('https://fantasy.premierleague.com/api/bootstrap-static/')
jsonResponse = r.json()
with open(file_path, 'w') as outfile: json.dump(jsonResponse, outfile)

# Run the function and choose where to save the json file
get_json('C:\Ste Files\Python\test\fpl.json')

# Open the json file and print a list of the keys

with open('C:\Ste Files\Python\test\fpl.json') as json_data: d = json.load(json_data)
print(list(d.keys()))
我希望用代码将文件写入路径。但我得到了以下错误:

(base) C:\Ste File\Python\test>python ste_test.py
Traceback (most recent call last):
File "ste_test.py", line 10, in <module>
jsonResponse = r.json()
NameError: name 'r' is not defined
(基本)C:\Ste File\Python\test>Python Ste\u test.py
回溯(最近一次呼叫最后一次):
文件“ste_test.py”,第10行,在
jsonResponse=r.json()
NameError:未定义名称“r”
这可能会欺骗你:

更改您的代码:

def get_json(file_path): r = requests.get('https://fantasy.premierleague.com/api/bootstrap-static/')
jsonResponse = r.json()
with open(file_path, 'w') as outfile: json.dump(jsonResponse, outfile)
与:

def get_json(file_path):
    r = requests.get('https://fantasy.premierleague.com/api/bootstrap-static/')
    jsonResponse = r.json()
    with open(file_path, 'w') as outfile: 
        json.dump(jsonResponse, outfile)
或:

这可能会欺骗你:

更改您的代码:

def get_json(file_path): r = requests.get('https://fantasy.premierleague.com/api/bootstrap-static/')
jsonResponse = r.json()
with open(file_path, 'w') as outfile: json.dump(jsonResponse, outfile)
与:

def get_json(file_path):
    r = requests.get('https://fantasy.premierleague.com/api/bootstrap-static/')
    jsonResponse = r.json()
    with open(file_path, 'w') as outfile: 
        json.dump(jsonResponse, outfile)
或:



考虑到这个错误,我不认为这里的缩进问题只是未能正确发布。那一行可能在函数之外。我不确定你的意思是什么?如果这正是你在ide中的代码,它会失败,因为它缺少缩进。python需要适当的缩进。首先看一些教程,您可能只需要正确缩进get_json函数的代码行。缩进很重要。您的
r.json()
不在
get_json
函数的范围内(因为缩进不正确),因此名称
r
未定义。考虑到错误,我不认为这里的缩进问题只是无法正确发布。那一行可能在函数之外。我不确定你的意思是什么?如果这正是你在ide中的代码,它会失败,因为它缺少缩进。python需要适当的缩进。首先看一些教程,您可能只需要正确缩进get_json函数的代码行。缩进很重要。您的
r.json()
不在
get_json
函数的范围内(因为缩进不正确),因此名称
r
未定义。缩进修复了一个错误,但我还有另一个错误。有人能让这个工作吗
python(base)C:\>cd C:\Ste Files\python\test(base)C:\Ste Files\python\test>python Ste_test.py回溯(最后一次调用):文件“Ste_test.py”,第16行,在get_json('C:\Ste Files\python\test\fpl.json'))中,文件“Ste_test.py”,第12行,在get_json中打开(文件路径,'w'),作为输出文件:OSError:[22]无效参数:“C:\\Ste Files\\Python\test\x0cpl.json”
您可以使用变量json\u path代替字符串“C:\Ste Files\Python\test\fpl.json:import os dir\u path=os.getcwd()json\u path=os.path.join(dir\u path,'fpl.json')json\u path谢谢,太棒了!那是一种享受。我现在唯一需要弄清楚的是如何提取玩家数据。本赛季周五开赛,伊基投了一票。我可以帮你做更多的事情吗?或者这个问题很接近?缩进修正了一个错误,但我还有另一个错误。有人能让这个工作吗
python(base)C:\>cd C:\Ste Files\python\test(base)C:\Ste Files\python\test>python Ste_test.py回溯(最后一次调用):文件“Ste_test.py”,第16行,在get_json('C:\Ste Files\python\test\fpl.json'))中,文件“Ste_test.py”,第12行,在get_json中打开(文件路径,'w'),作为输出文件:OSError:[22]无效参数:“C:\\Ste Files\\Python\test\x0cpl.json”
您可以使用变量json\u path代替字符串“C:\Ste Files\Python\test\fpl.json:import os dir\u path=os.getcwd()json\u path=os.path.join(dir\u path,'fpl.json')json\u path谢谢,太棒了!那是一种享受。我现在唯一需要弄清楚的是如何提取玩家数据。本赛季周五开赛,伊基投了一票。我有更多的事要我帮忙,还是这个问题就要解决了?