菜鸟笔记
提升您的技术认知

lxml库中etree.html()和etree.tostring()用法-ag真人游戏

1. 测试html代码

# 测试代码test.html
    
        
    
    
        

this is paragraph1

测试语句1

this is paragraph2

测试语句2

2. etree.html( )

调用html类对html文本进行初始化,成功构造xpath解析对象,同时可以自动修正hmtl文本(标签缺少闭合自动添加上)

from lxml import etree	#首先导入lxml库的etree模块
with open('test.html','r') as f:
    c = f.read()
#调用html类进行初始化,成功构造xpath解析对象
tree = etree.html(c)

3. etree.tostring()

tostring( )方法可以输出修正之后的html代码,也可以直接读取文本进行解析,但是结果为bytes类型,因此需要利用decode()方法将其转成str类型

具体的decode( )格式需要浏览器审查网页查看

import requests
from lxml import etree
with open('real_case.html', 'r', encoding='utf-8') as f:
    c = f.read()
tree = etree.html(c)
table_element = tree.xpath("//div[@class='table-box'][1]/table/tbody/tr")
for row in table_element:
    try:
        td1 = row.xpath('td')[0]
        #具体的转成什么格式,需要审查网页元素,查看
        s1 = etree.tostring(td1).decode('utf-8')
        print(s1)
    except exception as error:
        pass
网站地图