我需要从网页中提取一个部分。我需要一个版本与DOM和没有XPath。这是我的版本。需要从“最新分发”中提取并在浏览器中显示信息。
<?php
$result = file_get_contents ('https://distrowatch.com/');
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($result);
$xpath = new DOMXPath($doc);
$node = $xpath->query('//table[@class="News"]')->item(0);
echo $node->textContent;
发布于 2019-05-16 18:41:52
这看起来很简单,但是用XPath代替它是浪费时间。
<?php
$result = file_get_contents ('https://distrowatch.com/');
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($result);
foreach ($doc->getElementsByTagName("table") as $table) {
if ($table->getAttribute("class") === "News") {
echo $table->textContent;
break;
}
}
https://stackoverflow.com/questions/56174666
复制