This forum is now read-only. Please use our new forums at discuss.codecademy.com.

The NPR API Forum

View Course » View Exercise

913 points
C0646e8672143e52990d88469fde2ac5?s=140&d=retro
Submitted by
MizBlackCrow
about 5 years ago

Why won't this write to XML?

This is my current code. I'm mainly concerned with the fact that nothing seems to write to my_feed.xml, but printing output gives me a ton of XML data, so the search and reading portion seems to work.

What's weirder is, when I try to tab over to my_feed.xml, the Python code lingers on the screen, but I can't scroll, select, or alter (i.e. type) anything in that tab.

This code also passes.

What am I doing wrong here?

from urllib2 import urlopen
from urllib import quote

key = "API_KEY"
url = 'http://api.npr.org/query?apiKey='
url += key

numResults = "3"
url +="&numResults=" + numResults

url += "&action=Or"
#????????

requiredAssets = "audio"
url += "&requiredAssets=" + requiredAssets

format = "podcast"
url += "&format=" + format

npr_id = raw_input("Enter comma-separated NPR IDs or leave blank:")
search_string = raw_input("Enter your search string or leave blank:")
feed_title = raw_input("What's your feed title?")

if npr_id or search_string:
    raw_input("Hit Enter to download your podcast!!")
    if npr_id: 
        url += "&id=" + npr_id
    if search_string:
        url += "&searchTerm=" + quote(search_string)
    if feed_title:
        url += "&title=" + quote(feed_title)
    response = urlopen(url)
    output = response.read()
    my_feed = open("my_feed.xml", "w")
    my_feed.write(output)
    my_feed.close()
else: 
    print "You must enter an NPR ID, a search term, or both."

print url
print response

-3 votes

permalink

This code passed. Maybe it's helpful to you.

   from urllib2 import urlopen
   from urllib import quote

key = "API_KEY"
 url = 'http://api.npr.org/query?apiKey='
 url += key + '&numResults=3&action=or&requiredAssets=audio&format=Podcast'

 npr_id = raw_input("Enter comma-separated NPR IDs or leave blank." )
 search_string = raw_input("Enter your search string or leave blank.")
feed_title = raw_input("What's your feed title?")

if npr_id or search_string:
     raw_input("Hit Enter to download your podcast.")
    if npr_id:
       url = url + "&id=" + npr_id
    if search_string:
       url = url + "&searchTerm=" + quote(search_string)
    if feed_title:
        url = url + "&title="
        response = urlopen(url)
        output = response.read()
        my_feed
        my_feed.write(output)
         my_feed.close()
  else: 
     raw_input("You must enter an NPR ID, a search term, or both.")

2009 points
5039b870793452000202625b_865269212
Submitted by
eileenshen
almost 5 years ago