gpt4 book ai didi

python - 通过 XPath 解析 HTML

转载 作者:IT老高 更新时间:2023-10-28 22:16:35 28 4
gpt4 key购买 nike

在 .Net 中,我发现了这个很棒的库,HtmlAgilityPack这使您可以使用 XPath 轻松解析格式不正确的 HTML。我已经在我的 .Net 站点中使用了几年,但我不得不为我的 Python、Ruby 和其他项目适应更痛苦的库。有人知道其他语言的类似库吗?

最佳答案

我很惊讶没有提到 lxml。它速度极快,可以在任何允许 CPython 库的环境中工作。

这里是you can parse HTML via XPATH using lxml .

>>> from lxml import etree
>>> doc = '<foo><bar></bar></foo>'
>>> tree = etree.HTML(doc)

>>> r = tree.xpath('/foo/bar')
>>> len(r)
1
>>> r[0].tag
'bar'

>>> r = tree.xpath('bar')
>>> r[0].tag
'bar'

关于python - 通过 XPath 解析 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/285990/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com