【问题标题】:Dict not able to return data from college scorecard APIDict 无法从大学记分卡 API 返回数据
【发布时间】: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 上问过同样的问题,我有一个建议,所以我可以试一试。在任何其他情况下,我阅读了大学记分卡文档,“学校”是类别,其中一个属性称为“名称”。
  • 我刚试过你的建议,我收到“关键字不能是表达式”。

标签: python json api


【解决方案1】:

未经测试,在移动设备上编写,但我认为这应该与您在第一个请求中的内容相同。如果我是你,我会将下面的代码复制到一个新的 .py 文件中,然后运行它。这样,如果它引发错误,您就知道我的代码有问题(而不是代码的其他部分)。希望它有效或让您更接近解决方案。

import requests

key = "replace me with your API key"
url_base = "https://api.data.gov/ed/collegescorecard/v1/schools/"
p =  {"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}

resp = requests.get(url=url_base, params=p)
data = resp.json()
print data 

【讨论】:

  • 嘿冷!它似乎工作。我现在将专注于将 json 数据绑定到 HTML 文件...非常感谢您的耐心等待。你是一个巨大的帮助。 :)
  • 没问题。如果您对上述内容有任何疑问,请告诉我,如果我知道答案,我可以提供帮助。祝你好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多