gpt4 book ai didi

php - 使用 PHP DOM DOCUMENT 获取整个 BODY 内容

转载 作者:可可西里 更新时间:2023-11-01 12:29:55 26 4
gpt4 key购买 nike

我想使用 DOM 文档获取整个正文标签内容。

我使用了以下代码:

$dom = new domDocument;

/*** load the html into the object ***/
$dom->loadHTML($html);

/*** the table by its tag name ***/
$tables = $dom->getElementsByTagName('body')->item(0)->nodeValue;

这给了我 TExt。我想要全身内容。

最佳答案

您可以将主体 DOMElement 传递给 DOMDocument::saveHTML()DOMDocument::saveHTMLFile() ,例如

<?php
$doc = new DOMDocument;
$doc->loadhtmlfile('http://stackoverflow.com');

$body = $doc->getElementsByTagName('body');
if ( $body && 0<$body->length ) {
$body = $body->item(0);
echo $doc->savehtml($body);
}

打印

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : p in http://stackoverflow.com, line: 2843 [...]
<body class="home-page">
<noscript><div id="noscript-padding"></div></noscript>
<div id="notify-container"></div>
<div id="overlay-header"></div>
<div id="custom-header"></div>
<div class="container">
<div id="header">
<div id="portalLink">
[...]

关于php - 使用 PHP DOM DOCUMENT 获取整个 BODY 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8968394/

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