【问题标题】:PHP parsing invalid htmlPHP解析无效的html
【发布时间】:2011-02-11 18:24:43
【问题描述】:

我正在尝试解析一些不在我的服务器上的 html

    $dom = new DOMDocument();
    $dom->loadHTMLfile("http://www.some-site.org/page.aspx");      
    echo    $dom->getElementById('his_id')->item(0);

但是 php 返回类似ID his_id already defined in http://www.some-site.org/page.aspx, line: 33 的错误。我认为这是因为 DOMDocument 正在处理无效的 html。那么,即使无效,我该如何解析它?

【问题讨论】:

    标签: php html-parsing domdocument


    【解决方案1】:

    你应该在解析它之前运行HTML Tidy来清理它。

    $html = file_get_contents('http://www.some-site.org/page.aspx');
    $config = array(
      'clean' => 'yes',
      'output-html' => 'yes',
    );
    $tidy = tidy_parse_string($html, $config, 'utf8');
    $tidy->cleanRepair();
    $dom = new DOMDocument;
    $dom->loadHTML($tidy);
    

    看到这个list of options

    【讨论】:

    • tidy 不适合我 :(
    • @kmunky 为什么不呢?如果没有 Tidy,基本上你就是 SOL。
    • 我解决了问题...我已经安装了 php_tidy 但我收到以下错误“ID top already defined in Entity, line: 52”
    • 重复的 id,你必须自己修复它们(去过那里,做过)。
    【解决方案2】:

    阅读文档时,我看到默认为 TRUE 的 $dom->strictErrorChecking。如果你设置$dom->strictErrorChecking = false会发生什么?

    【讨论】:

      【解决方案3】:

      看看:libxml_use_internal_errors()

      http://php.net/libxml_use_internal_errors

      【讨论】:

      • 如果您只是要推荐一个链接,请在问题下方发表评论,而不是作为答案发布。
      猜你喜欢
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多