gpt4 book ai didi

php - 你如何解析和处理 PHP 中的 HTML/XML?

转载 作者:行者123 更新时间:2023-12-01 16:45:38 24 4
gpt4 key购买 nike

如何解析 HTML/XML 并从中提取信息?

最佳答案

原生 XML 扩展
我更喜欢使用 native XML extensions 之一由于它们与 PHP 捆绑在一起,因此通常比所有 3rd 方库都快,并为我提供了对标记所需的所有控制。
DOM

The DOM extension allows you to operate on XML documents through the DOM API with PHP 5. It is an implementation of the W3C's Document Object Model Core Level 3, a platform- and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure and style of documents.


DOM 能够解析和修改现实世界(损坏的)HTML,它可以做 XPath queries .它基于 libxml .
使用 DOM 需要一些时间来提高效率,但 IMO 这段时间非常值得。由于 DOM 是一种与语言无关的接口(interface),您会发现多种语言的实现,因此如果您需要更改您的编程语言,那么您很可能已经知道如何使用该语言的 DOM API。
基本用法示例可以在 Grabbing the href attribute of an A element 中找到。可以在 DOMDocument in php 找到一般概念概述。
How to use the DOM extension has been covered extensively on StackOverflow ,所以如果你选择使用它,你可以确定你遇到的大部分问题都可以通过搜索/浏览 Stack Overflow 来解决。
XMLReader

The XMLReader extension is an XML pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.


XMLReader 和 DOM 一样,也是基于 libxml 的。我不知道如何触发 HTML 解析器模块,因此使用 XMLReader 解析损坏的 HTML 的可能性可能不如使用 DOM 强,您可以在其中明确告诉它使用 libxml 的 HTML 解析器模块。
可以在 getting all values from h1 tags using php 找到基本用法示例。
XML Parser

This extension lets you create XML parsers and then define handlers for different XML events. Each XML parser also has a few parameters you can adjust.


XML Parser 库也是基于libxml,并实现了 SAX样式 XML 推送解析器。它可能是比 DOM 或 SimpleXML 更好的内存管理选择,但比 XMLReader 实现的拉式解析器更难使用。
SimpleXml

The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.


当您知道 HTML 是有效的 XHTML 时,SimpleXML 是一个选项。如果您需要解析损坏的 HTML,甚至不要考虑 SimpleXml,因为它会卡住。
可以在 A simple program to CRUD node and node values of xml file 找到基本用法示例。还有 lots of additional examples in the PHP Manual .

第 3 方库(基于 libxml)
如果您更喜欢使用第 3 方库,我建议您使用实际使用 DOM 的库。/ libxml在下面而不是字符串解析。
FluentDom - Repo

FluentDOM provides a jQuery-like fluent XML interface for the DOMDocument in PHP. Selectors are written in XPath or CSS (using a CSS to XPath converter). Current versions extend the DOM implementing standard interfaces and add features from the DOM Living Standard. FluentDOM can load formats like JSON, CSV, JsonML, RabbitFish and others. Can be installed via Composer.


HtmlPageDom

Wa72\HtmlPageDom is a PHP library for easy manipulation of HTMLdocuments using DOM. It requires DomCrawler from Symfony2components for traversingthe DOM tree and extends it by adding methods for manipulating theDOM tree of HTML documents.


phpQuery (多年未更新)

phpQuery is a server-side, chainable, CSS3 selector driven Document Object Model (DOM) API based on jQuery JavaScript Library written in PHP5 and provides additional Command Line Interface (CLI).


另见: https://github.com/electrolinux/phpquery
Zend_Dom

Zend_Dom provides tools for working with DOM documents and structures. Currently, we offer Zend_Dom_Query, which provides a unified interface for querying DOM documents utilizing both XPath and CSS selectors.


QueryPath

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources. It implements much of the jQuery interface (including CSS-style selectors), but it is heavily tuned for server-side use. Can be installed via Composer.


fDOMDocument

fDOMDocument extends the standard DOM to use exceptions at all occasions of errors instead of PHP warnings or notices. They also add various custom methods and shortcuts for convenience and to simplify the usage of DOM.


sabre/xml

sabre/xml is a library that wraps and extends the XMLReader and XMLWriter classes to create a simple "xml to object/array" mapping system and design pattern. Writing and reading XML is single-pass and can therefore be fast and require low memory on large xml files.


FluidXML

FluidXML is a PHP library for manipulating XML with a concise and fluent API.It leverages XPath and the fluent programming pattern to be fun and effective.



3rd-Party(不是基于 libxml 的)
构建在 DOM/libxml 上的好处是,您可以获得良好的开箱即用性能,因为您基于 native 扩展。然而,并不是所有的 3rd-party libs 都走这条路。其中一些列在下面
PHP Simple HTML DOM Parser
  • An HTML DOM parser written in PHP5+ lets you manipulate HTML in a very easy way!
  • Require PHP 5+.
  • Supports invalid HTML.
  • Find tags on an HTML page with selectors just like jQuery.
  • Extract contents from HTML in a single line.

我一般不推荐这个解析器。代码库很糟糕,解析器本身很慢而且很耗内存。并非所有 jQuery 选择器(例如 child selectors )都可用。任何基于 libxml 的库都应该轻松胜过这一点。
PHP Html Parser

PHPHtmlParser is a simple, flexible, html parser which allows you to select tags using any css selector, like jQuery. The goal is to assiste in the development of tools which require a quick, easy way to scrape html, whether it's valid or not! This project was original supported by sunra/php-simple-html-dom-parser but the support seems to have stopped so this project is my adaptation of his previous work.


同样,我不会推荐这个解析器。 CPU 使用率高时速度相当慢。也没有清除创建的 DOM 对象内存的功能。这些问题尤其适用于嵌套循环。文档本身不准确且拼写错误,自 16 年 4 月 14 日以来没有对修复程序作出回应。
Ganon
  • A universal tokenizer and HTML/XML/RSS DOM Parser
  •    Ability to manipulate elements and their attributes
  •    Supports invalid HTML and UTF8
  •    Can perform advanced CSS3-like queries on elements (like jQuery -- namespaces supported) 
  • A HTML beautifier (like HTML Tidy)
  •    Minify CSS and Javascript
  •    Sort attributes, change character case, correct indentation, etc. 
  • Extensible
  •    Parsing documents using callbacks based on current character/token
  •    Operations separated in smaller functions for easy overriding 
  • Fast and Easy

从来没有用过。说不上好不好。

HTML 5
你可以使用上面的来解析 HTML5,但是 there can be quirks由于 HTML5 允许的标记。因此,对于 HTML5,您要考虑使用专用解析器,例如
html5lib

A Python and PHP implementations of a HTML parser based on the WHATWG HTML5 specification for maximum compatibility with major desktop web browsers.


一旦 HTML5 完成,我们可能会看到更多的专用解析器。 W3 还有一篇博文,标题为 How-To for html 5 parsing值得一试。

网络服务
如果您不想编写 PHP,也可以使用 Web 服务。一般来说,我发现这些用途很少,但这只是我和我的用例。
ScraperWiki .

ScraperWiki's external interface allows you to extract data in the form you want for use on the web or in your own applications. You can also extract information about the state of any scraper.



正则表达式
最后和 最不推荐 ,您可以使用 regular expressions 从 HTML 中提取数据.通常不鼓励在 HTML 上使用正则表达式。
您将在网络上找到的大多数与标记匹配的片段都很脆弱。在大多数情况下,它们仅适用于非常特殊的 HTML 片段。微小的标记更改,例如在某处添加空格,或添加或更改标签中的属性,都可能导致 RegEx 在编写不正确时失败。在 HTML 上使用 RegEx 之前,您应该知道自己在做什么。
HTML 解析器已经知道 HTML 的语法规则。必须为您编写的每个新 RegEx 教授正则表达式。 RegEx 在某些情况下很好,但这实际上取决于您的用例。
can write more reliable parsers ,但是当上述库已经存在并且在这方面做得更好时,使用正则表达式编写完整可靠的自定义解析器是浪费时间。
另见 Parsing Html The Cthulhu Way

书籍
如果你想花一些钱,看看
  • PHP Architect's Guide to Webscraping with PHP

  • 我不隶属于 PHP 架构师或作者。

    关于php - 你如何解析和处理 PHP 中的 HTML/XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/292926/

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