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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With