HTMLParser python turn data into string?
Question: Hi I'm using this following python parser to read an html file

<https://docs.python.org/2/library/htmlparser.html>

    
    
    class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.inLink = False
        self.dataArray = []
        self.countLanguages = 0
        self.lasttag = None
        self.lastname = None
        self.lastvalue = None
    
    def handle_starttag(self, tag, attrs):
        self.inLink = False
        if tag == 'window':
            for name, value in attrs:
                if name == 'mode':
                    #value = 0
                    #print(value)
                    self.countLanguages += 1
                    self.inLink = True
                    self.lasttag = tag
    
    def handle_endtag(self, tag):
        if tag == "window":
            self.inlink = False
    
    def handle_data(self, data):
        self.data = data
        #print(self.data)
        print data
        if data.strip():
            self.inlink = False
            #print data
    
      parser = MyHTMLParser()
      input_file = open('xmlfile.xml')
      feed_data = input_file.read().strip()
      feed_data = parser.feed(feed_data)
      print(feed_data.data)
      input_file.close()
    

However, I cant seem to find a way to turn the data returned from html parser
into a string.

I'm basically modyfing an attribute using html parser, then I want to turn the
data into a string - any idea how to do that?

I've tried adding a "self.data" to the "handle_data" function, but I cant seem
to print any data coming back. Is there a way to just print everything held in
the parser?


Answer: This is another way that might suit your needs for this specific question:

    
    
    #!/usr/bin/env python3 
    # -*- coding: utf-8 -*- 
    
    import urllib.request 
    url = "http://www.google.com"
    
    
    with urllib.request.urlopen(url) as response:
        html = response.read().decode('utf-8')
    
    print (html)
    

