Simple_html_dom不返回<h1>元素


simple_html_dom not returning <h1> elements?

我正在测试使用SIMPLE_HTML_DOM的解析器,而解析从这个URL返回的HTML DOM: HERE

没有找到H1元素…我尝试返回所有的div成功。

我用一个简单的请求来诊断这个问题:

foreach($html->find('H1') as $value) { echo "<br />F: ".htmlspecialchars($value); } 

在查看源代码时,我意识到:

  • h1是大写-> h1 -但是SIMPLE_HTML…正在处理

                //PaperG - If lowercase is set, do a case insensitive test of the value of the selector.
            if ($lowercase) {
                $check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue));
            } else {
                $check = $this->match($exp, $val, $nodeKeyValue);
            }
            if (is_object($debugObject)) {$debugObject->debugLog(2, "after match: " . ($check ? "true" : "false"));}
    

有谁能告诉我这是怎么回事吗?

Try This

        $oHtml = str_get_html($html);
        foreach($oHtml->find('h1') as $element)
        {
            echo $element->innertext;
        }

您还将使用正则表达式下面的函数返回所有h1标记的innertext的数组

  function getH1($yourhtml)
{
    $h1tags = preg_match_all("/(<h1.*>)('w.*)(<'/h1>)/isxmU", $yourhtml, $patterns);
    $res    = array();
    array_push($res, $patterns[2]);
    array_push($res, count($patterns[2]));
    return $res;
}

找到了…

但无法解释!

我用另一个包含H1(大写)的代码测试,它工作了。

在玩SIMPLE_HTML_DOM代码时,我注释了"remove_noise",现在它工作了很好,我认为这是因为这个网站有无效的HTML和噪声去除器删除了太多,并且没有在结束标签脚本之后结束:

    // $this->remove_noise("'<'s*script[^>]*[^/]>(.*?)<'s*/'s*script's*>'is");
    // $this->remove_noise("'<'s*script's*>(.*?)<'s*/'s*script's*>'is");

谢谢大家的帮助。