2

I am trying to make a python 3.3 program to download a XML file, parse it, and display the info. I know how to parse the file but I am having downloading the file and converting it to parse-able XML. Here is what I have so far.

import xml.etree.ElementTree as ET
import webbrowser,time,urllib.request
import tkinter as tk

# webbrowser.get('windows-default').open_new('http://www.reddit.com/'+'r/blender')

class Application(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.send_entry = tk.Entry(self)
        self.send_entry.grid(row=0,column=0)
        self.change_sub = tk.Button(self,text='Change Subreddit', command=lambda :self.getXML(self.send_entry.get())).grid(row=0 , column=1)

        self.QUIT = tk.Button(self, text="QUIT", fg="red", command=main.destroy).grid(row=2)

    def getXmlData(self):
        tree=ET.parse('rss.xml')
        root=tree.getroot()
        for child in root:
            print(child)
    def getXML(self,subreddit):

        url = 'http://www.reddit.com'+subreddit+'.rss'
        response=urllib.request.urlopen(url)
        data = response.read()

        # print(xmlString)
        with open('rss.xml','wb') as self.xml:
            self.xml.write(data)

main = tk.Tk()
# main.geometry("250x150")
app = Application(master=main)
app.mainloop()

After trying to figure it by myself for quite awhile I know that is the characters that are really messing with it. IE when it runs and downloads it converts some characters to ascii( I think). Any help would be great

1 Answer 1

1

instead of using urlopen I just use and retrieve.

def getXML(self,subreddit):
        url = 'http://www.reddit.com'+subreddit+'.rss'
        source = urllib.request.urlretrieve(url,'rss.xml')
        self.getXmlData()
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure what tkinter has in the way of progress bars but if you give the urlretrieve method a "hook" function, the hook function will receive arguments regarding how far along the download process it is. You can pass those arguments along to a progress bar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.