【问题标题】:Parse Website for URLs解析网站的 URL
【发布时间】:2011-05-26 13:20:43
【问题描述】:

只是想知道是否有人可以在以下方面进一步帮助我。我要解析这个网站的网址:http://www.directorycritic.com/free-directory-list.html?pg=1&sort=pr

我有以下代码:

<?PHP  
$url = "http://www.directorycritic.com/free-directory-list.html?pg=1&sort=pr";
$input = @file_get_contents($url) or die("Could not access file: $url"); 
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
if(preg_match_all("/$regexp/siU", $input, $matches)) { 
// $matches[2] = array of link addresses 
// $matches[3] = array of link text - including HTML code
} 
?>

目前什么都不做,我需要做的是废弃表中所有 16 个页面的所有 URL,并且非常感谢有关如何修改上述内容以执行此操作并将 URL 输出到文本文件中的一些帮助。

【问题讨论】:

标签: php html parsing html-parsing


【解决方案1】:

使用HTML Dom Parser

$html = file_get_html('http://www.example.com/');

// Find all links
$links = array(); 
foreach($html->find('a') as $element) 
       $links[] = $element->href;

现在 links 数组包含给定页面的所有 URL,您可以使用这些 URL 进一步解析。

用正则表达式解析 HTML 不是一个好主意。以下是一些相关帖子:

编辑:

下面的 cmets 中 Gordon 描述的一些其他 HTML 解析工具:

【讨论】:

【解决方案2】:

你真的不应该使用正则表达式来解析 HTML,因为它很容易出错。

最好使用像PHP’s DOM library这样的HTML解析器:

$code = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($code);
$links = array();
foreach ($doc->getElementsByTagName('a') as $element) {
    if ($element->hasAttribute('href')) {
        $links[] = $elements->getAttribute('href');
    }
}

请注意,这将收集文档中出现的 URI 引用,而不是绝对 URI。您可能需要先解决它们。

似乎 PHP 没有提供合适的库(或者我还没有找到)。但请参阅RFC 3986 – Reference Resolutionmy answer on Convert a relative URL to an absolute URL with Simple HTML DOM? 了解更多详情。

【讨论】:

    【解决方案3】:

    试试这个方法

    function getinboundLinks($domain_name) {
    ini_set('user_agent', 'NameOfAgent (<a class="linkclass" href="http://localhost">http://localhost</a>)');
     $url = $domain_name;
    $url_without_www=str_replace('http://','',$url);
    $url_without_www=str_replace('www.','',$url_without_www);
     $url_without_www= str_replace(strstr($url_without_www,'/'),'',$url_without_www);
    $url_without_www=trim($url_without_www);
    $input = @file_get_contents($url) or die('Could not access file: $url');
     $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
    //$inbound=0;
    $outbound=0;
    $nonfollow=0;
    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
    # $match[2] = link address
     # $match[3] = link text
    //echo $match[3].'<br>';
    if(!empty($match[2]) && !empty($match[3])) {
    if(strstr(strtolower($match[2]),'URL:') || strstr(strtolower($match[2]),'url:') ) {
    $nonfollow +=1;
    } else if (strstr(strtolower($match[2]),$url_without_www) || !strstr(strtolower($match[2]),'http://')) {
         $inbound += 1;
        echo '<br>inbound '. $match[2];
     }
    else if (!strstr(strtolower($match[2]),$url_without_www) && strstr(strtolower($match[2]),'http://')) {
    echo '<br>outbound '. $match[2];
         $outbound += 1;
        }
    }
    }
    }
    $links['inbound']=$inbound;
    $links['outbound']=$outbound;
    $links['nonfollow']=$nonfollow;
    return $links;
    }
    
    // ************************Usage********************************
    $Domain='<a class="linkclass" href="http://zachbrowne.com">http://zachbrowne.com</a>';
    $links=getinboundLinks($Domain);
    echo '<br>Number of inbound Links '.$links['inbound'];
    echo '<br>Number of outbound Links '.$links['outbound'];
    echo '<br>Number of Nonfollow Links '.$links['nonfollow'];
    

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 2015-10-23
      • 2014-09-27
      • 2015-10-05
      • 2014-12-19
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多