抓取图片

 <?php

function geturl($url){
	
	$ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $content = curl_exec($ch);
		
        curl_close($ch);
	
		
if(preg_match_all('/(?<=src=").*?.(jpg|jpeg|gif|png|bmp|JPG|JPEG|GIF|PNG|BMP)/',$content,$a)){ //匹配字串中是否包至少两位到4位的数字
    echo "匹配!";
	
	$f=implode("|",$a[0]);
$array=explode('|', $f);
$t=0;
 for($r=0;$r<strlen($f);$r++){
	 if(substr($f,$r,1)=="|"){
		 $t++;
		 echo "<img src=".$array[$t]." /><br>";
	 }

 }
echo "<hr>";
echo $f;
 $fp2 = fopen('a.html', 'w');
    fwrite($fp2,$f);
    fclose($fp2);
}else{
    echo "不匹配!";
}
		
	
}

function getFile($url, $save_dir = '', $filename = '', $type = 0) {
    if (trim($url) == '') {
        return false;
    }
    if (trim($save_dir) == '') {
        $save_dir = './';
    }
   
    //创建保存目录
    if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
        return false;
    }
    //获取远程文件所采用的方法
    if ($type) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $content = curl_exec($ch);
        curl_close($ch);
    } else {
        ob_start();
        readfile($url);
        $content = ob_get_contents();
        ob_end_clean();
    }
	//echo $content;
    $size = strlen($content);
    //文件大小
    $fp2 = @fopen($save_dir . $filename, 'a');
    fwrite($fp2, $content);
    fclose($fp2);
    unset($content, $url);
    return array(
        'file_name' => $filename,
        'save_path' => $save_dir . $filename,
		'file_size' => $size
    );
}
//$url = "http://www.baidu.com/img/baidu_jgylogo3.gif";
$url="http://www.pc6.com/";
$arr=parse_url($url);
echo $host = $arr['host'];
//$url="http://192.168.1.212/aaa.doc";
//$url="http://192.168.31.1/".urlencode(iconv("GB2312","UTF-8","测试.doc"));
$save_dir = "down/";
//$filename = "baidu_jgylog1o31.gif";
$filename ="123.html";
$res = getFile($url, $save_dir, $filename,1);//0  1 都是好使的
geturl($url);
var_dump($res);
?>

评论

刷新

友情链接