【发布时间】: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> </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代码:
-
然后呢?您已经有一个工作代码可以引用所有所需的
<td>s,只需迭代 theirchildNodes即可走到您想要的任何节点。 -
我这样做了,但是
-
请演示您是如何尝试提取
<img>s 的。
标签: php dom extract domdocument text-extraction