This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
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
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.")
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