lxml包没有etree模块和parse报错处理

  • lxml包没有etree模块的解决方法:

环境:python3.7+ lxml4.4.4

因为etree是C语言写的,所以在import时,不会有提示,直接输入即可

from lxml import etree

 

  • 在使用etree.parse时报错,原因:该方法默认使用的是“XML”解析器,所以如果碰到不规范的html文件时就会解析错误

htmlElement = etree.parse('renren.html')
  File "src\lxml\etree.pyx", line 3467, in lxml.etree.parse
  File "src\lxml\parser.pxi", line 1839, in lxml.etree._parseDocument
  File "src\lxml\parser.pxi", line 1865, in lxml.etree._parseDocumentFromURL
  File "src\lxml\parser.pxi", line 1769, in lxml.etree._parseDocFromFile
  File "src\lxml\parser.pxi", line 1163, in lxml.etree._BaseParser._parseDocFromFile
  File "src\lxml\parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
  File "src\lxml\parser.pxi", line 711, in lxml.etree._handleParseResult
  File "src\lxml\parser.pxi", line 640, in lxml.etree._raiseParseError
  File "renren.html", line 4
lxml.etree.XMLSyntaxError: StartTag: invalid element name, line 4, column 2

解决办法:

自己创建html解析器,增加parser参数

    parser = etree.HTMLParser(encoding='utf-8')
    htmlElement = etree.parse('renren.html', parser=parser)

 

etree很多方法不会提示,直接手动输入即可:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
    功能:lxml和xpath使用
    环境:python3.7+ lxml4.4.4
    日期:2019/8/14 21:41
    作者:指尖魔法师
    版本:1.0
"""

from lxml import etree

def fromstring():
    text = '''
    
    '''
    htmlElement = etree.HTML(text)
    result = etree.tostring(htmlElement, encoding='utf-8').decode('utf-8')
    print(result)


def fromfile():
    #默认是XML解析器,碰到不规范的html文件时就会解析错误,增加解析器
    parser = etree.HTMLParser(encoding='utf-8')
    htmlElement = etree.parse('renren.html', parser=parser)
    result = etree.tostring(htmlElement, encoding='utf-8').decode('utf-8')
    print(result)




def main():
    #字符串读取html
    #fromstring()

    # 从文件读取html
    fromfile()


if __name__ == '__main__':
    main()

 

你可能感兴趣的:(Python爬虫)