1.etree.parse Accept a document directly , Parse according to the document structure ( Local files )
test.html:
<html>
<body>
<ul>
<li><a href="http//www.baidu.com"> Baidu </a></li>
<li><a href="http//www.sougou.com"> sogou </a></li>
<li><a href="http//www.baihe.com"> Lily </a></li>
</ul>
<ol>
<li><a href="wzc">wzc</a></li>
<li><a href="kwq">kwq</a></li>
<li><a href="baby">baby</a></li>
</ol>
</body>
</html>
tree = etree.parse("test.html")
# One sentence query href
print(tree.xpath("/html/body/ul/li/a/@href"))
# Relative query
href_list = tree.xpath("/html/body/ul/li")
for href in href_list:
result2 = href.xpath("./a/@href")
print(result2)
2.etree.html Can be parsed html file :( Returned from the server html data )
url = "https://www.baidu.com/"
resp = requests.get(url)
page = etree.HTML(resp.text)