python3-module对象不可调用

时间:2014-03-10 13:46:23

标签: python python-2.7 python-3.x

crawl.py。我尝试将其移植到python3。我遗漏了所有不必要的细节。 错误:

Traceback (most recent call last):
  File "crawl.py", line 44, in parseAndGetLinks
    self.parser = html.parser(AbstractFormatter(DumbWriter(StringIO())))
TypeError: 'module' object is not callable


import html.parser  
from formatter import DumbWriter, AbstractFormatter  
from io  import StringIO     

parser = html.parser(AbstractFormatter(DumbWriter(StringIO())))

1 个答案:

答案 0 :(得分:5)

html.parser是模块;你想要那个模块中的HTMLParser class

parser = html.parser.HTMLParser(AbstractFormatter(DumbWriter(StringIO())))

或更简洁:

from html.parser import HTMLParser

parser = HTMLParser(AbstractFormatter(DumbWriter(StringIO())))