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

本文详细介绍了在Python环境中使用lxml库与XPath进行网页解析的方法。针对不规范的HTML文件,通过自定义HTML解析器来避免解析错误,展示了如何从字符串和文件中读取HTML并进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 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 = '''
    <div>
        <ul>
            <li class="item-0"><a href="link1.html">first item</a></li>
            <li class="item-1"><a href="link3.html">second item</a></li>
            <li class="item-inactive"><a href="link4.html">third item</a></li>
            <li class="item-1"><a href="link4.html">fourth item</a></li>
            <li class="item-0"><a href="link5.html">fifth item</a></li>
        </ul>
    </div>
    '''
    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()

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值