您好我想從紐約時報API「標題」後能得到的所有信息,這裏是我的代碼紐約時報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
如何解決問題?
在你的url中,你製作了api鍵'##'而不是一些字符串。也許這可能是問題所在? – Michael0x2a 2013-05-08 01:43:21