【发布时间】:2018-08-19 00:21:55
【问题描述】:
所以我一直在处理一个通过大学记分卡 API 显示学校名称、网址、城市、州、邮编和学生人数的页面,但我却遇到了大量错误。但是,该程序能够很好地读取 JSON 数据。例如,当我运行这个时:
key = "key_string_here"
url_base = "https://api.data.gov/ed/collegescorecard/v1/schools/"
# Makes a get request to collegescorecard API
r = requests.get("https://api.data.gov/ed/collegescorecard/v1/schools/?
school.operating=1&2015.academics.program_available.assoc_or_bachelors=true&
2015.student.size__range=1..&school.degrees_awarded.predominant__range=1..3
&school.degrees_awarded.highest__range=2..4&id=240444&api_key=api_key_here")
school = r.json()
for item in school:
url = ("https://api.data.gov/ed/collegescorecard/v1/schools?"
"school.operating=1&2015.academics.program_available"
".assoc_or_bachelors=true&2015.student.size__range=1.."
"&school.degrees_awarded.predominant__range=1..3"
"&school.degrees_awarded.highest__range=2..4&id=240444&"
"api_key="+key+"&fields=school.name, school.school_url,"
"school.city,school.zip,school.state,2015.student.size")
req = urllib2.Request(url)
response = urllib2.urlopen(req)
response2 = response.read()
json_data=json.loads(response2)
print response2
我得到了正确的数据:
{"metadata":{"total":1,"page":0,"per_page":20},"results":[{"school.n
ame":"University of Wisconsin-Madison","school.zip":"53706-1380","sc
hool.state":"WI","2015.student.size":29579,"school.school_url":"www.
wisc.edu","school.city":"Madison"}]}
但是,当我尝试解析字典中的 JSON 数据时,如下所示:
params = dict(
school_name="University of Wisconsin-Madison",
school_url="www.wisc.edu",
city="Madison",
state="WI",
zip="53706-1380",
size="29579"
)
resp = requests.get(url=url, params=params)
data = resp.json()
print data
我得到这个回应:
{u'errors': [{u'input': u'city', u'message': u"The input parameter '
city' is not known in this dataset.", u'error': u'parameter_not_foun
d'}, {u'input': u'state', u'message': u"The input parameter 'state'
is not known in this dataset.", u'error': u'parameter_not_found'}, {
u'input': u'school_url', u'message': u"The input parameter 'school_u
rl' is not known in this dataset.", u'error': u'parameter_not_found'
}, {u'input': u'school_name', u'message': u"The input parameter 'sch
ool_name' is not known in this dataset.", u'error': u'parameter_not_
found'}, {u'input': u'size', u'message': u"The input parameter 'size
' is not known in this dataset.", u'error': u'parameter_not_found'},
{u'input': u'53706-1380', u'message': u"The provided zipcode, '5370
6-1380', is not valid.", u'parameter': u'zip', u'error': u'zipcode_e
rror'}]}
我做错了什么?我该如何解决这个问题?
奖励:有什么改进我的网页的建议吗?
回应寒冷:
vagrant@vagrant:/vagrant/scripts$ python school_data.py
File "school_data.py", line 29
"school_url"="www.wisc.edu",
SyntaxError: keyword can't be an expression
编辑二:
File "school_data.py", line 28
school_url: "www.wisc.edu",
^
SyntaxError: invalid syntax
编辑三:
vagrant@vagrant:/vagrant/scripts$ python school_data.py
File "school_data.py", line 28
"school.school_url"="www.wisc.edu",
SyntaxError: keyword can't be an expression
【问题讨论】:
-
在您的第一个示例(为您提供正确的数据)中,参数的格式为
school.name,但在您传递给params的字典中为school_name。两者在语义上不等价,也许这就是问题所在? -
我尝试将“school.name”作为字典中的变量名,但在运行程序后,收到一个错误,指出“学校”(或“名称”。我都试过了。没有工作。)未定义。
-
我认为这是因为它认为您有一个名为
school的对象,并且它认为您正在尝试访问它的某些方法/属性,称为name。您是否能够将字典键括在引号中并将其作为字符串传递?即params = {"school.name" : "University of Wisconsin-Madison", "school.url" : "www.wisc.edu"}(本词典不完整,需要填写剩余参数)。 -
是的,我相信我试过了,但也没有用。我在 Reddit 上问过同样的问题,我有一个建议,所以我可以试一试。在任何其他情况下,我阅读了大学记分卡文档,“学校”是类别,其中一个属性称为“名称”。
-
我刚试过你的建议,我收到“关键字不能是表达式”。