【发布时间】:2016-01-06 10:20:20
【问题描述】:
我是 Python 新手。我尝试运行此代码,但收到 ImportError 的错误消息:没有名为“HTMLParser”的模块。我正在使用 Python 3.x。为什么这不起作用?
#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()
我收到以下错误:
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'
【问题讨论】:
-
docs.python.org/2/library/htmlparser.html - "HTMLParser 模块在 Python 3 中已重命名为 html.parser"
标签: python-3.x