Python:ImportError:没有名为' HTMLParser'

时间:2016-01-06 10:20:20

标签: python-3.x

我是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'

1 个答案:

答案 0 :(得分:40)

该模块在Python 3中称为html.parser。因此,您需要更改导入以反映该新名称:

var jsdom = require("jsdom");
var async = require("async");
var schCD = require('./ggElemCD.json');


async.eachSeries(schCD, function(item, callback){

    jsdom.env(
        "http://www.~~skipped~~&HG_CD=" + item.HG_CD + "~~skipped~~", 
        ["http://code.jquery.com/jquery.js"],
        function(err, window){

            var tObj= window.$('.TableType1 tbody tr:last td');

            console.log( "Name : " + item.HG_NM);
            console.log( "Code : " + item.HG_CD);

            var completeObj = {
                "HG_name" : item,
                "HG_CD"   : item.HG_CD,
                "G1st_m"  : tObj.get(0).innerHTML,
                "G1st_f"  : tObj.get(1).innerHTML,
                "G2nd_m"  : tObj.get(3).innerHTML,
                "G2nd_f"  : tObj.get(4).innerHTML,
                "G3rd_m"  : tObj.get(6).innerHTML,
                "G3rd_f"  : tObj.get(7).innerHTML,
                "G4th_m"  : tObj.get(9).innerHTML,
                "G4th_f"  : tObj.get(10).innerHTML,
                "G5th_m"  : tObj.get(12).innerHTML,
                "G5th_f"  : tObj.get(13).innerHTML,
                "G6th_m"  : tObj.get(15).innerHTML,
                "G6th_f"  : tObj.get(16).innerHTML,
                "sp_m"    : tObj.get(18).innerHTML,
                "sp_f"    : tObj.get(19).innerHTML
            };

            console.log("value1 : " + completeObj.G1st_m);
            console.log("value2 : " + completeObj.G1st_f);

            callback(err);
        }
    );
}, function (err){

    console.log("Done.");
    err && console.log(err);
});

您应该始终检查standard library documentation以确保从正确的位置导入正确的内容。