W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
from lxml import etree
html=etree.parse('test.html',etree.HTMLParser()) #指定解析器HTMLParser會根據(jù)文件修復HTML文件中缺失的如聲明信息
result=etree.tostring(html) #解析成字節(jié)
#result=etree.tostringlist(html) #解析成列表
print(type(html))
print(type(result))
print(result)
#
<class 'lxml.etree._ElementTree'>
<class 'bytes'>
b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">\n<html><body><div> \n <ul> \n <li class="item-0"><a href="link1.html">first item</a></li>
\n <li class="item-1"><a href="link2.html">second item</a></li>
\n <li class="item-inactive"><a href="link3.html">third item</a></li>
\n <li class="item-1"><a href="link4.html">fourth item</a></li>
\n <li class="item-0"><a href="link5.html">fifth item</a> \n </li></ul> \n </div> \n</body></html>'
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: