NPR API 6 - print story titles


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

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


#2

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!


#3

from urllib2 import *
from json import *
url ="http://api.npr.org/query?apikey="
key = "API_KEY"
url += key
url ='?numResults=3&format=json&id=1001'
print url
response = urlopen(url)
json_obj=response.load()
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"


#4

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

Change

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".