php curl采集页面内容并提取所有的链接

四月 06, 2019 | views
Comments 0

提取链接是一个很简单的做法了,下面这个例子相对来讲是比较全面了,下面我们一起来看看这个php curl采集页面内容并提取所有的链接例子.

本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集,一般php采集网络数据会用file_get_contents、file和cURL,不过据说cURL会比file_get_contents、file更快更专业,更适合采集,今天就试试用cURL来获取网页上的所有链接,示例如下:

  1. <?php 
  2. /* 
  3.  * 使用curl 采集phpfensi.com下的所有链接。 
  4.  */ 
  5. include_once('function.php'); 
  6. $ch = curl_init(); 
  7. curl_setopt($ch, CURLOPT_URL, 'http://www.phpfensi.com/'); 
  8. // 只需返回HTTP header 
  9. curl_setopt($ch, CURLOPT_HEADER, 1); 
  10. // 页面内容我们并不需要 
  11. // curl_setopt($ch, CURLOPT_NOBODY, 1); 
  12. // 返回结果,而不是输出它 
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  14. $html = curl_exec($ch); 
  15. $info = curl_getinfo($ch); 
  16. if ($html === false) { 
  17.  echo "cURL Error: " . curl_error($ch); 
  18. curl_close($ch); 
  19. $linkarr = _striplinks($html); 
  20. // 主机部分,补全用 
  21. $host = 'http://www.phpfensi.com/'
  22. if (is_array($linkarr)) { 
  23.  foreach ($linkarr as $k => $v) { 
  24.   $linkresult[$k] = _expandlinks($v$host); 
  25.  } 
  26. printf("<p>此页面的所有链接为:</p><pre>%s</pre>n", var_export($linkresult , true)); 
  27. ?> 

function.php内容如下,即为上两篇中两个函数的合集:

  1. <?php 
  2. function _striplinks($document) { 
  3.  preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s>]+))'isx", $document$links); 
  4.  // catenate the non-empty matches from the conditional subpattern 
  5.  while (list($key$val) = each($links[2])) { 
  6.   if (!emptyempty($val)) 
  7.    $match[] = $val
  8.  } while (list($key$val) = each($links[3])) { 
  9.   if (!emptyempty($val)) 
  10.    $match[] = $val
  11.  } 
  12.  // return the links 
  13.  return $match
  14. /*===================================================================* 
  15.  Function: _expandlinks 
  16.  Purpose: expand each link into a fully qualified URL 
  17.  Input:  $links   the links to qualify 
  18.     $URI   the full URI to get the base from 
  19.  Output:  $expandedLinks the expanded links 
  20. *===================================================================*/ 
  21. function _expandlinks($links,$URI
  22.  $URI_PARTS = parse_url($URI); 
  23.  $host = $URI_PARTS["host"]; 
  24.  preg_match("/^[^?]+/",$URI,$match); 
  25.  $match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]); 
  26.  $match = preg_replace("|/$|","",$match); 
  27.  $match_part = parse_url($match); 
  28.  $match_root = 
  29.  $match_part["scheme"]."://".$match_part["host"]; 
  30.  $search = array(  "|^http://".preg_quote($host)."|i"
  31.       "|^(/)|i"
  32.       "|^(?!http://)(?!mailto:)|i"
  33.       "|/./|"
  34.       "|/[^/]+/../|" 
  35.      ); 
  36.  $replace = array""
  37.       $match_root."/"
  38.       $match."/"
  39.       "/"
  40.       "/" 
  41.      ); 
  42.  $expandedLinks = preg_replace($search,$replace,$links); 
  43.  return $expandedLinks
  44. ?> 

具体想要和file_get_contents做一个比较的话,可以利用linux下的time命令查看两者执行各需多长时间,据目前测试看是CURL更快一些,最后链接下上面两个函数相关介绍.

匹配链接函数:function _striplinks()

相对路径转绝对:function _expandlinks()



zend