Dict无法从大学记分卡API返回数据

时间:2018-08-19 00:21:55

标签: python json api

因此,我一直在研究一个页面,该页面通过College Scorecard API显示学校的名称,URL,城市,州,邮编和学生人数,但是却出现了很多错误。但是,该程序可以读取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'}]}

我在做什么错?我该如何解决这个问题?

奖金:对改善我的网页有何建议?

响应chillin

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

编辑II:

  File "school_data.py", line 28
    school_url: "www.wisc.edu",
              ^
SyntaxError: invalid syntax

编辑III:

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

1 个答案:

答案 0 :(得分:0)

未经测试,写在手机上,但是我认为这应该与您第一次请求中的内容相同。如果是您,我将下面的代码复制到一个新的.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