我有大约。我需要从中提取信息的 40k+ html 文档。我尝试使用 PHP+Tidy(因为大多数文件格式不正确)+DOMDocument+XPath 来这样做,但它非常慢......我被建议使用正则表达式,但 html 文件没有语义标记(表基于布局,到处都使用无意义的标签/类)而且我不知道我应该从哪里开始......
只是好奇,使用 regexp (PHP/Python) 是否比使用 Python 的 XPath 库更快? Python 的 Xpath 库通常比 PHP 的对应库更快吗?
最佳答案
如果需要速度,请查看 lxml . lxml 是 libxml2 的 pythonic 绑定(bind)和 libxslt C 库。使用 C 库比任何纯 php 或 python 版本都快得多。
有一些令人印象深刻的benchmarks来自伊恩比金:
In Conclusion
I knew lxml was fast before I started these benchmarks, but I didn’t expect it to be quite this fast.
解析结果:
Parsing Resutls http://1.2.3.9/bmi/blog.ianbicking.org/wp-content/uploads/images/parsing-results.png
关于php - 使用 PHP(XPath)、PHP/Python(Regex) 或 Python(XPath) 从 html 中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1553511/