NPR API 6 - print story titles

from urllib2 import urlopen
from json import load   
response = urllib2.urlopen(url)
json_obj = json.load(response)
for story in json_obj['list']['story']:

I get the error saying: urllib2 is not defined. Why is that?


With your 'response' variable you've already imported 'urlopen' from the 'urllib2' module, but not the whole module, so it won't be defined. In this case you don't need to type the module name again:

response = urlopen(url)

same idea for the json variable below that.
Also for the print line in your 'for' loop you'll need to write it the same way as in the instructions:

print story['title']['$text']

Hope this helps!


from urllib2 import *
from json import *
url =""
key = "API_KEY"
url += key
url ='?numResults=3&format=json&id=1001'
print url
response = urlopen(url)
for story in json_obj['list']['story']:
print story['title']['$text']
the error is" Traceback (most recent call last):
File "python", line 8, in
ValueError: unknown url type: ?numResults=3&format=json&id=1001"


Add(+) a symbol & change a symbol (hint hint) in


use response = urlopen(url) as an example of how to call load() on response

If you are still stuck, I suggest looking back at the first lesson in the NPR API course titled "What You'll Be Building".