Question

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

Était-ce utile?

La solution

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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top