【问题标题】:Extracting certain portions of HTML from within PHP从 PHP 中提取 HTML 的某些部分
【发布时间】:2012-03-16 22:33:38
【问题描述】:

好的,所以我正在用 PHP 编写一个应用程序来检查我的网站是否所有链接都有效,所以如果需要我可以更新它们。

我遇到了一个问题。我尝试使用 SimpleXml 和 DOMDocument 对象来提取标签,但是当我使用示例站点运行应用程序时,如果我使用 SimpleXml 对象类型,通常会出现大量错误。

那么有没有一种方法可以像使用 SimpleXml 一样简单地扫描 html 文档中的 href 属性?

    <?php
    // what I want to do is get a similar effect to the code described below:

    foreach($html->html->body->a as $link)
    {
         // store the $link into a file
         foreach($link->attributes() as $attribute=>$value);
         {
              //procedure to place the href value into a file
         }
    }
?>

所以基本上我正在寻找一种方法来执行上述操作。问题是我目前对如何处理我得到的带有 html 代码的字符串感到困惑......

为了清楚起见,我使用以下原始方式获取 html 文件:

<?php
$target      = "http://www.targeturl.com";

$file_handle = fopen($target, "r");

$a = "";

while (!feof($file_handle)) $a .= fgets($file_handle, 4096);

fclose($file_handle);
?>

任何信息以及任何其他可以更优雅地解决上述问题的语言替代方案(python、c 或 c++)都会很有用

【问题讨论】:

    标签: php web-crawler html-parsing simplexml domdocument


    【解决方案1】:

    您可以使用DOMDocument::loadHTML

    这是我们编写的用于 HTML 解析工具的一堆代码。

    $target = "http://www.targeturl.com";
    $result = file_get_contents($target);
    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = false;
    @$dom->loadHTML($result);
    
    $links = extractLink(getTags( $dom, 'a', ));
    
    function extractLink( $html, $argument = 1 ) {
      $href_regex_pattern = '/<a[^>]*?href=[\'"](.*?)[\'"][^>]*?>(.*?)<\/a>/si';
    
      preg_match_all($href_regex_pattern,$html,$matches);
    
      if (count($matches)) {
    
        if (is_array($matches[$argument]) && count($matches[$argument])) {
          return $matches[$argument][0];
        }
    
        return $matches[1];
      } else 
    
    function getTags( $dom, $tagName, $element = false, $children = false ) {
        $html = '';
        $domxpath = new DOMXPath($dom);
    
        $children = ($children) ? "/".$children : '';  
        $filtered = $domxpath->query("//$tagName" . $children);
    
        $i = 0;
        while( $myItem = $filtered->item($i++) ){
            $newDom = new DOMDocument;
            $newDom->formatOutput = true;        
    
            $node = $newDom->importNode( $myItem, true );
    
            $newDom->appendChild($node);
            $html[] = $newDom->saveHTML();          
        }
    
        if ($element !== false && isset($html[$element])) {
          return $html[$element];
        } else
          return $html;
    } 
    

    【讨论】:

    • 很好,我一定会试一试,需要查找一些东西,但我认为现在我知道我需要查找的内容不会成为问题
    • DOMDocument 和 DOMXPath 非常棒,即使对于糟糕/损坏的 HTML 也非常宽容。你可以用它做很多事情:)
    • 是的,我刚刚开始尝试 DOMXPath,它看起来很有趣。但是,我需要更深入的文档,然后是 php.net 提供的文档,这些示例并不像我希望的那样提供丰富的信息。
    【解决方案2】:

    您可以只使用strpos($html, 'href='),然后解析网址。您也可以搜索&lt;a.php

    【讨论】:

    • 我需要一些更优雅的东西,sonassi 的上述解决方案(如我所见)为该问题提供了更优雅的解决方案,但感谢您的尝试:)
    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 2017-06-26
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多