下面是一个链接爬网程序,它可以获取给定深度的页面URL。最后,我添加了一个正则表达式来匹配刚刚爬网的url的所有电子邮件。正如您在第二部分中看到的,它file_get_content
与刚刚下载的页面相同,这意味着执行时间、带宽等是的两倍
问题是,我如何合并这两个部分来使用第一个下载的页面,以避免再次获得它?非常感谢。
function crawler($url, $depth = 2) {
$dom = new DOMDocument('1.0');
if (!$parts || !@$dom->loadHTMLFile($url)) {
return;
}
.
.
.
//this is where the second part starts
$text = file_get_contents($url);
$res = preg_match_all("/[a-z0-9]+([_''.-][a-z0-9]+)*@([a-z0-9]+(['.-][a-z0-9]+)*)+''.[a-z]{2,}/i", $text, $matches);
}
替换:
$text = file_get_contents($url);
带有:
$text = $dom->saveHTML();
http://www.php.net/manual/en/domdocument.savehtml.php
或者,在函数的第一部分中,可以使用file_get_contents
将HTML保存到一个变量中,然后将其传递给$dom->loadHTML
。这样,您就可以在正则表达式中重用该变量。
http://www.php.net/manual/en/domdocument.loadhtml.php