温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在PHP中使用CURL抓取定时任务

发布时间:2021-02-07 20:58:43 来源:亿速云 阅读:240 作者:Leah 栏目:开发技术

本篇文章为大家展示了如何在PHP中使用CURL抓取定时任务,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<?php
 function getLink($url){
  include_once('simple_html_dom.php');
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_HEADER,false);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  $output = curl_exec($ch);
  curl_close($ch);
  $html = new simple_html_dom();
  $html->load($output);
 $links = array();
  $arr = array();
 $title = array();
  foreach($html->find('a') as $element){
   if(preg_match('#^\/content_[0-9]+_1\.html$#i',$element->href)){
      array_push($links,'https://www.jb51.net'.$element->href);
  array_push($title,$element->title);
 }
 }
 $links = array_values(array_unique($links));
 $title = array_values(array_unique($title));
 $arr['links'] = $links;
 $arr['title'] = $title;
 return $arr;
 }
 function loadimg($url,$dirname){
 include_once('simple_html_dom.php');
 $ch = curl_init();
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_HEADER,false);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 $output = curl_exec($ch);
 curl_close($ch);
 $html = new simple_html_dom();
 $html->load($output);
 $arr = array();
 foreach($html->find('img[w]') as $element){
  $image = $element->src;
 }
 $data = file_get_contents($image);
  $info = getimagesize($image);//获取图片信息,大小,格式
  switch($info[2]){
   case 1:
    $str = 'gif';
    break;
   case 2:
    $str = 'jpg';
    break;
   case 3:
    $str = 'png';
    break;
   default:
    continue;
    break;
  }
  if($info[1] < 10 || $info[0] < 10) continue;//图片太小,不是有价值的图片,跳过本次循环
  $filename = time().rand(1,999999).'.'.$str;
  if(!is_dir($dirname)){
   mkdir($dirname,0777,true);
  }
  $fp = fopen($dirname.$filename,'w');
  fwrite($fp,$data);
  fclose($fp);
  return $dirname.$filename;
}
 do{
  set_time_limit(0);
  ignore_user_abort();
  $img = getLink('https://www.jb51.net/qutu_1.html');
  $count = count($img['links']);
  $arr = array();
  for($i=0;$i<$count;$i++){
  $arr[]=loadimg($img['links'][$i],'images/');
  }
  $img['url'] = $arr;
  echo '<br/>';
  $img['title'];
  $res = array();
  $len = count($img['title']);
  //重新将数据组装成我们常用的二维数组,方便数据的数据库处理
  for($i=0;$i<$len;$i++){
   $res[$i]['title'] = $img['title'][$i];
  $res[$i]['url'] = $img['url'][$i];
  }
  foreach($res as $item){
   echo '<img src='.$item["url"].'>'.$item["title"].'<br />';
  }
  $interval = 24*3600;
  sleep($interval);
  }while(true);
?>

上述内容就是如何在PHP中使用CURL抓取定时任务,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI