4
我想解析這個XML(http://www.reddit.com/r/videos/top/.rss),並有麻煩這樣做。我試圖保存每個項目中的YouTube鏈接,但由於「頻道」子節點而遇到麻煩。我如何達到這個水平,然後我可以遍歷這些項目?如何使用python解析xml提要?
#reddit parse
reddit_file = urllib2.urlopen('http://www.reddit.com/r/videos/top/.rss')
#convert to string:
reddit_data = reddit_file.read()
#close file because we dont need it anymore:
reddit_file.close()
#entire feed
reddit_root = etree.fromstring(reddit_data)
channel = reddit_root.findall('{http://purl.org/dc/elements/1.1/}channel')
print channel
reddit_feed=[]
for entry in channel:
#get description, url, and thumbnail
desc = #not sure how to get this
reddit_feed.append([desc])