This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by MizBlackCrow
almost 11 years

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

Answer 51d2b0cf7c82ca52280098a9

-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.")
points
Submitted by eileenshen
almost 11 years