繁体   English   中英

php dom xpath从站点中所有文件夹中提取所有链接

[英]php dom xpath extract all links from all folders in site

我已经在stackoverflow和网络上进行了搜索,在这里一定缺少一些东西。 我没有找到我想要的东西。 也许它叫别的东西..我在下面的这段代码可以捕获第一个文件夹中的所有内容,但不会捕获其他文件夹中的其他项目..例如,它可以捕获第一个文件夹/中的所有内容,但是如果您有站点mysite。 com / folder2 /,它将不会抓取folder2。 一切都联系在一起。 它也确实会向后移动。 如果您放置网站的最长链接,则会一直到网站的最前面。 我不确定我缺少什么指针会很棒。 该网站是我要删除的joomla网站。

<?php function storelink($web,$taken) {
$query = "INSERT INTO scanned (web, taken) VALUES ('$web', '$taken')";
mysql_query($query) or die('Error, insert query failed');
  }

   $target_web = "mysite.com";
  $userAgent = 'bobsbot(http://www.somebot.com/bot.html)';

 // make the cURL request to $target_web
 $ch = curl_init();

  curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
 curl_setopt($ch, CURLOPT_URL, $target_web);
 curl_setopt($ch, CURLOPT_FAILONERROR, true);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
 curl_setopt($ch, CURLOPT_AUTOREFERER, true);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
 curl_setopt($ch, CURLOPT_TIMEOUT, 1000);



  $html= curl_exec($ch);
  if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}

    // parse the html into a DOMDocument
  $dom = new DOMDocument();
   @$dom->loadHTML($html);

  // grab all the on the page
  $xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

  for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$web = $href->getAttribute('href');
storeLink($web,$target_web);
echo "<br />Link saved: $web";




 } ?>

如果我对您的理解正确,那么您想建立一个网站并保存所有URL。 这意味着您遇到URL时需要递归。

用于启动蜘蛛程序的函数称为saveLink($web, $taken) 遇到链接时调用的函数是storeLink($web, $target_web) 那不应该是saveLink($web, $target_web)吗?

saveLink()应该是递归的,并且还要执行cURL请求。 cURL URL应该设置为遇到的链接。 这样,它将解析所有遇到的链接的DOM,并跟踪其中的所有链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM