PHP抓取网页图片的实例
|
set_time_limit(0); $URL='http://image.baidu.com/';//任意网址 get_pic($URL); function get_pic($pic_url) { $data=CurlGet($pic_url); $pattern_src = '//'; $num = preg_match_all($pattern_src, $data, $match_src); $arr_src=$match_src[1]; print_r($arr_src); get_name($arr_src); echo " finished!!!"; return 0; } function get_name($pic_arr) { $pattern_type = '/(.(jpg|bmp|jpeg|gif|png))/'; foreach($pic_arr as $pic_item){ $num = preg_match_all($pattern_type, $pic_item, $match_type); if($num){ $pic_name = get_unique().$match_type[1][0]; $write_fd = @fopen('./getpic/'.$pic_name,"wb"); @fwrite($write_fd, CurlGet($pic_item)); @fclose($write_fd); echo "[OK]..!"; } } return 0; } function get_unique(){ list($msec, $sec) = explode(" ",microtime()); return $sec.intval($msec*1000000); } function CurlGet($url){ $url=str_replace('&','&',$url); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)"); curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); $values = curl_exec($curl); curl_close($curl); return $values; } ?> |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1591/viewspace-2810967/
,如需转载,请注明出处,否则将追究法律责任。