程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Etree HTML and etree What is the difference between parse

編輯:Python

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)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved