解析网站以获取URL

6

请问是否有人可以帮我进一步解决以下问题。我想解析此网站的URL: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,并将其输出到一个文本文件中。我真的很需要帮助,想知道如何修改上述代码以实现这一点。


2
(相关) 解析HTML的最佳方法 - Gordon
可能是抓取A元素href属性的正则表达式的重复问题。 - Gordon
3个回答

5
使用 HTML Dom解析器
$html = file_get_html('http://www.example.com/');

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

现在,链接数组包含给定页面的所有URL,您可以使用这些URL进行进一步解析。
使用正则表达式解析HTML不是一个好主意。以下是一些相关文章:
- 使用正则表达式解析HTML:为什么不行? - RegEx匹配开放标签,除了XHTML自包含标签 编辑:
以下是评论中由Gordon描述的其他HTML解析工具:
- phpQuery - Zend_Dom - QueryPath - FluentDom

1
建议使用第三方替代品 SimpleHtmlDom,实际上使用 DOM 而不是字符串解析的有:phpQueryZend_DomQueryPathFluentDom - Gordon
@Gordon:是的,你说得对。我想我们之前都回答过同类型的问题。 - Naveed

3

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

最好使用HTML解析器,比如PHP的DOM库中的解析器:

$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 - 参考解析我在使用Simple HTML DOM将相对URL转换为绝对URL时的答案获取更多详细信息。

0

尝试这个方法

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'];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接