Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: ImportError: No module named 'HTMLParser' [duplicate]

Tags:

I am new to Python. I have tried to ran this code but I am getting an error message for ImportError: No module named 'HTMLParser'. I am using Python 3.x. Any reason why this is not working ?

#Import the HTMLParser model from HTMLParser import HTMLParser  #Create a subclass and override the handler methods class MyHTMLParser(HTMLParser):  #Function to handle the processing of HTML comments     def handle_comment(self,data):         print ("Encountered comment: ", data)         pos = self.getpos()         print ("At line: ", pos[0], "position ", pos[1])  def main():     #Instantiate the parser and feed it some html     parser= MyHTMLParser()      #Open the sample file and read it     f = open("myhtml.html")     if f.mode== "r":         contents= f.read()  #read the entire FileExistsError         parser.feed()   if __name__== "__main__":     main() 

I am getting the following error:

Traceback (most recent call last):   File "C:\Users\bm250199\workspace\test\htmlparsing.py", line 3, in <module>     from HTMLParser import HTMLParser ImportError: No module named 'HTMLParser' 
like image 523
user2625433 Avatar asked Jan 06 '16 10:01

user2625433


1 Answers

The module is called html.parser in Python 3. So you need to change your import to reflect that new name:

from html.parser import HTMLParser 

You should always check the standard library documentation to make sure that you are importing the right things from the right location.

like image 162
poke Avatar answered Oct 21 '22 12:10

poke