【问题标题】:DomDocument php extract info and imagesDomDocument php 提取信息和图像
【发布时间】:2023-04-02 23:30:01
【问题描述】:

您好,我遇到了 DomDocument 问题。我需要编写一个脚本,从具有特定 ID 的表中提取所有信息。

所以我做到了:

$link = "WEBSITE URL";

$html = file_get_contents($link);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$context_nodes = $xpath->query('//table[@id="news"]/tr[position()>0]/td');

所以我得到了所有<td>s 和信息,但问题是脚本没有提取<img> 标签。如何提取表格的所有信息,无论是文本还是图像 html 标记?

我要从中提取信息的 html 代码是:

<table id="news" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td width="539" height="35"><span><strong>Info to Extract</strong></span></td>
    </tr>
    <tr>
        <td height="35" class="texto10">Martes, 02 de Octubre de 2012  | Autor: Trovert"  rel="author"></a></td>
    </tr>
    <tr>
        <td height="35" class="texto12Gris"><p><strong>Info To extract</strong></p>
            <p><strong>&nbsp;</strong></p>
            <p><strong>Casa de Gobierno: (a 9 cuadras del hostel)</strong></p>
            <img title="title" src="../images/theimage.jpg" width="400" height="266" />
        </td>
    </tr>
</table>

这就是我迭代提取的元素的方式:

foreach ($context_nodes as $node) {
    echo $node->nodeValue . '<br/>';
}

谢谢

【问题讨论】:

  • 您能否提供一个最低限度的 HTML 示例并演示您要提取的内容?
  • 感谢您的回答,这里有一些html代码:
  • 然后呢?您已经有一个工作代码可以引用所有所需的 &lt;td&gt;s,只需迭代 their childNodes 即可走到您想要的任何节点。
  • 我这样做了,但是
  • 请演示您是如何尝试提取&lt;img&gt;s 的。

标签: php dom extract domdocument text-extraction


【解决方案1】:

如果你需要的不仅仅是文本,你就必须更加努力,不仅仅是nodeValue/textContent,而是遍历目标节点的DOM分支:

function walkNode($node)
{
    $str="";
    if($node->nodeType==XML_TEXT_NODE)
    {
        $str.=$node->nodeValue;
    }
    elseif(strtolower($node->nodeName)=="img")
    {
        /* This is just a demonstration;
         * You'll have to extract the info in the way you want
         * */
        $str.='<img src="'.$node->attributes->getNamedItem("src")->nodeValue.'" />';
    }
    if($node->firstChild) $str.=walkNode($node->firstChild);
    if($node->nextSibling) $str.=walkNode($node->nextSibling);
    return $str;
}

这是一个简单直接的递归函数。所以现在你可以这样做了:

$dom=new DOMDocument();
$dom->loadHTML($html);
$xpath=new DOMXPath($dom);
$tds=$xpath->query('//table[@id="news"]//tr[position()>0]/td');
foreach($tds as $td)
{
    echo walkNode($td->firstChild);
    echo "\n";
}

Online demo
(请注意,我“修复”了您的一些 HTML,因为它似乎无效;也缩进了一点)

这会输出如下内容:

Info to Extract
Martes, 02 de Octubre de 2012  | Autor: Trovert
Info To extract

            Casa de Gobierno: (a 9 cuadras del hostel)
            <img src="../images/theimage.jpg" />

【讨论】:

  • 非常感谢。实际上这可行,但它会删除所有标签,如 等。我怎样才能提取所有标签?谢谢
  • @Gonzalo 那么你必须更清楚你真正想要什么。你的问题只是说你想要“信息”(这只是你的代码的文本)加上&lt;img&gt;标签,所以这就是我所做的。你的意思是你想要&lt;td&gt;s 的“innerHTML”部分?
【解决方案2】:

试试这个....

foreach ($context_nodes as $node) {
echo $doc->saveHTML($node) . '<br/>';
}

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2019-07-26
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多