嗨,我想从 NYtimes API 获取“标题”之后的所有信息,这是我的代码
from urllib2 import urlopen
from json import loads
import codecs
import time
def call_the_articles():
url = "http://api.nytimes.com/svc/search/v1/article?query=US&facets=POLITICS&api-key=##"
return loads(urlopen(url).read())
articles = call_the_articles()
if __name__ == '__main__':
for story in articles("results"):
print story['title'].encode('ascii', 'replace')
但是当我在终端中运行时,出现的错误如下:
File "NYtimes.py", line 10, in <module>
articles = call_the_articles()
File "NYtimes.py", line 8, in call_the_articles
return loads(urlopen(url).read())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 406, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 444, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
如何解决问题?