Python中的XPath表達式用法
XPath是一種用於在XML和HTML文件中進行導航和尋找的語言,廣泛應用於資料抓取、Web自動化測試、文字擷取等領域。在Python中,我們可以使用lxml函式庫來解析XML和HTML文檔,並使用XPath表達式來定位和擷取所需的資料。
pip install lxml
from lxml import etree
parser = etree.HTMLParser()
tree = etree.parse('example.html', parser)
xpath_expr = '//a'
nodes = tree.xpath(xpath_expr)
texts = [node.text for node in nodes] print(texts)
from lxml import etree parser = etree.HTMLParser() tree = etree.parse('example.html', parser) xpath_expr = '//a' nodes = tree.xpath(xpath_expr) links = [node.get('href') for node in nodes] print(links)
以上是Python中的XPath表達式用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!