无法从PHP中的网页获取h1

无法从PHP中的网页获取h1,php,Php,我想在第页得到公司的名称。 我所尝试的: <?PHP $html = file_get_contents('https://www.goudengids.be/bedrijf/Willebroek/L11159413/CNC+Metal/'); $document = new DOMDocument; $document ->loadHTML($html); $xPath = new DOMXPath($document); $anchorTags = $xPath->eval

我想在第页得到公司的名称。 我所尝试的:

<?PHP
$html = file_get_contents('https://www.goudengids.be/bedrijf/Willebroek/L11159413/CNC+Metal/');
$document = new DOMDocument;
$document ->loadHTML($html);
$xPath = new DOMXPath($document);
$anchorTags = $xPath->evaluate("//div[@class=\"title-logo\"]//h1");
foreach ((array)$anchorTags  as $anchorTag) {
    echo 'name : '.$anchorTag;
}
?>
我为另一个网站做了类似这样的事情,它成功了,但实际上数组$anchorTags似乎是空的。问题在哪里?
谢谢。

您要查找的xpath是:

//div[contains(@class,'title-logo')]//h1

simple@class不行

您不需要强制转换XPath evaluate方法的结果以在foreach中使用,您还需要获取I Aspect以获取标头标记的实际内容

foreach ($anchorTags  as $anchorTag) {
    echo 'name : '.$anchorTag->nodeValue;
}
将输出

name : CNC Metal
这对我很有用:

$html = file_get_contents('https://www.goudengids.be/bedrijf/Willebroek/L11159413/CNC+Metal/');
$document = new DOMDocument;
@$document->loadHTML($html); // using @ here to suppress a warning

$headings = $document->getElementsByTagName('h1');
foreach ($headings as $node) {
    echo 'name : '.$node->nodeValue;
}

这将给出与OP的XPath表达式相同的结果。