php - 用于解析 HTML 的 DOMDocument(而不是正则表达式)

标签 php parsing dom xpath

我正在尝试学习使用 DOMDocument 来解析 HTML 代码。

我只是在做一些简单的工作,我已经喜欢戈登在scrap data using regex and simplehtmldom上的回答了并根据他的工作编写了我的代码。

我发现 PHP.net 上的文档不太好,因为信息有限,几乎没有示例,而且大多数细节都是基于解析 XML。

<?php
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://www.nu.nl/internet/1106541/taalunie-keurt-open-sourcewoordenlijst-goed.html');
libxml_clear_errors();

$recipe = array();
$xpath = new DOMXPath($dom);
$contentDiv = $dom->getElementById('page'); // would have preferred getContentbyClass('content') (unique) in this case.

# title
print_r($xpath->evaluate('string(div/div/div/div/div/h1)', $contentDiv));

# content (this is not working)
#print_r($xpath->evaluate('string(div/div/div/div['content'])', $contentDiv)); // if only this worked
print_r($xpath->evaluate('string(div/div/div/div)', $contentDiv));
?>

出于测试目的,我试图获取 nu.nl 新闻文章的标题(h1 标签之间)和内容(HTML)。

如您所见,我可以获得标题,尽管我对该评估字符串不太满意,因为它恰好是该 div 级别上唯一的 h1 标签。

最佳答案

以下是如何使用 DOM 和 XPath 来做到这一点:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTMLFile('http://www.nu.nl/…');
libxml_clear_errors();

$xpath = new DOMXPath($dom);
echo $xpath->evaluate('string(id("leadarticle")/div/h1)');
echo $dom->saveHtml(
    $xpath->evaluate('id("leadarticle")/div[@class="content"]')->item(0)
);

XPath string(id("leadarticle")/div/h1) 将返回 h1 的 textContent,该 h1 是 div 的子级,而 div 是具有 id Leadarticle 的元素的子级.

XPath id("leadarticle")/div[@class="content"] 将返回带有 class 属性 content 的 div,该属性是 id 为 Leadarticle 的元素的子元素。

因为您想要内容 div 的外层 HTML,所以您必须获取整个节点而不仅仅是内容,因此没有 string() function in the XPath 。将节点传递给 DOMDocument::saveHTML()方法 ( which is only possible as of 5.3.6 ) 然后将该节点序列化回 HTML。

关于php - 用于解析 HTML 的 DOMDocument(而不是正则表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7324620/

相关文章:

php - SQL 删除在数组 : 中找不到的标签

php - 无法连接到 GoDaddy 上的 MySQL 数据库

c# - 日期时间c#解析

asp.net - 解析来自磁条的信用卡输入

javascript - 为什么当发送 AJAX req.js 的函数时,JS 生成的表单会在表单生成后立即触发 AJAX 请求。绑定(bind)到onsubmit事件?

javascript - jQuery 1.8 中的 DOM 插入导致 javascript 触发两次

php - 转换 BBCode 文本时检查图像大小

javascript - CKeditor 在粘贴事件时删除除 div、span 之外的所有 html 标签

javascript - 如何在JS中查找页面上的符号并获取两个符号之间的值?

javascript - DOM:我无法访问动态创建的元素。怎么了