This forum is now read-only. Please use our new forums! Go to forums
Cant find whats wrong
from urllib2 import urlopen from json import load, dumps
url = ‘http://api.npr.org/query?apiKey=‘
key = ‘API_KEY’
url = url + key
url += ‘&numResults=1&format=json&id=1007&requiredAssets=text
,image,audio’ #1007 is science
response = urlopen(url) json_obj = load(response)
for story in json_obj[‘list’][‘story’]: print “TITLE: “ + story[‘title’][‘$text’] + “\n”
print "DATE:" + story['storyDate']['$text'] + "\n"
print "TEASER:" + story['teaser']['$text'] + "\n"
if "byline" in story:
print "BYLINE:" + story['byline'][0]['$text']
Oops, try again. u”Check the line where you print ‘DATE:’. Don’t forget a ‘\n’ at the end of each line.”
Answer 51abb21a7133e26a91000add
Answer 51d48adf282ae31ad0000c87
Well, here’s my functioning code:
from urllib2 import urlopen
from json import load, dumps
url = 'http://api.npr.org/query?apiKey='
key = 'API_KEY'
url = url + key
url += '&numResults=1&format=json&id=1007' #1007 is science
url += "&requiredAssets=audio,text,image"
response = urlopen(url)
json_obj = load(response)
# uncomment 3 lines below to see JSON output to file
#f = open('output.json', 'w')
#f.write(dumps(json_obj, indent=4))
#f.close()
for story in json_obj['list']['story']:
print "TITLE: " + story['title']['$text'] + "\n"
print "DATE: " + story['storyDate']['$text'] + "\n"
print "TEASER: " + story['teaser']['$text'] + "\n"
if "byline" in story:
print "BYLINE:" + story['byline'][0]['name']['$text'] + "\n"
Answer 5182455a47dc9f6851002087
please use the ‘code indent button’ to format code as code:
Answer 51e629a28c1cccd22c00213b
I’ve noticed a lot of people running in to the same problem. What I ended up doing is cutting and pasting my code in to a text editor.
Once I did that, I found there was an extra indentation in the part of the code that worked previously.
I’m guession a lot of people are running in to these hidden “extras”.
Do yourself a favor and paste your code in to something that can handle plain text and you should find issues the editor won’t show you.
Good luck!
Answer 53a60a4c80ff33732200302b
By yg gy TV TV TV TV. TV TV TV TV gy hi I'm. from urllib2 import urlopen
from json import load, dumps
url = ‘http://api.npr.org/query?apiKey=‘ key = ‘API_KEY’ url = url + key url += ‘&numResults=1&format=json&id=1007’ #1007 is science url += “&requiredAssets=audio,text,image” response = urlopen(url) json_obj = load(response)
uncomment 3 lines below to see JSON output to file
#f = open(‘output.json’, ‘w’) #f.write(dumps(json_obj, indent=4)) #f.close()
for story in json_obj[‘list’][‘story’]: print “TITLE: “ + story[‘title’][‘$text’] + “\n” from urllib2 import urlopen from json import load, dumps
url = ‘http://api.npr.org/query?apiKey=‘ key = ‘API_KEY’ url = url + key url += ‘&numResults=1&format=json&id=1007’ #1007 is science url += “&requiredAssets=audio,text,image” response = urlopen(url) json_obj = load(response)
uncomment 3 lines below to see JSON output to file
#f = open(‘output.json’, ‘w’) #f.write(dumps(json_obj, indent=4)) #f.close()
for story in json_obj[‘list’][‘story’]: print “TITLE: “ + story[‘title’][‘$text’] + “\n”
print “DATE: “ + story[‘storyDate’][‘$text’] + “\n” print “TEASER: “ + story[‘teaser’][‘$text’] + “\n”
if “byline” in story:from urllib2 import urlopen from json import load, dumps
url = ‘http://api.npr.org/query?apiKey=‘ key = ‘API_KEY’ url = url + key url += ‘&numResults=1&format=json&id=1007’ #1007 is science url += “&requiredAssets=audio,text,image” response = urlopen(url) json_obj = load(response)
uncomment 3 lines below to see JSON output to file
#f = open(‘output.json’, ‘w’) #f.write(dumps(json_obj, indent=4)) #f.close()
for story in json_obj[‘list’][‘story’]: print “TITLE: “ + story[‘title’][‘$text’] + “\n”
print “DATE: “ + story[‘storyDate’][‘$text’] + “\n” print “TEASER: “ + story[‘teaser’][‘$text’] + “\n”
if “byline” in story: print “BYLINE:” + story[‘byline’][0][‘name’][‘$text’] + “\n” print “BYLINE:” + story[‘byline’][0][‘name’][‘$text’] + “\n” print “DATE: “ + story[‘storyDate’][‘$text’] + “\n” print “TEASER: “ + story[‘teaser’][‘$text’] + “\n”
if “byline” in story: print “BYLINE:” + story[‘byline’][0][‘name’][‘$text’] + “\n”
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
2 comments
tried it, didnt work
Are you sure you did + “\n” not + “/n” which is what I’ve been doing for like the past 1/2 hour? Also try changing “byline” to ‘byline’.