$URL='http://pp.baidu.com/';//任意網(wǎng)址

get_p " /> 成人午夜三级,欧美午夜免费,亚洲毛片一区二区

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼

復(fù)制代碼 代碼如下:
<?php
/*
author: ssh_kobe
date: 20110602
shortage: 如果網(wǎng)頁(yè)中的圖片路徑不是絕對(duì)路徑,就無(wú)法抓取
*/
set_time_limit(0);//抓取不受時(shí)間限制

$URL='http://pp.baidu.com/';//任意網(wǎng)址

get_pic($URL);

function get_pic($pic_url) {
//獲取圖片二進(jìn)制流
$data=CurlGet($pic_url);
/*利用正則表達(dá)式得到圖片鏈接*/
$pattern_src = '/<[img|IMG].*?src=[/'|/"](.*?(?:[/.gif|/.jpg]))[/'|/"].*?[//]?>/';
$num = preg_match_all($pattern_src, $data, $match_src);
$arr_src=$match_src[1];//獲得圖片數(shù)組
get_name($arr_src);

echo "<br>finished!!!";
return 0;
}

/*得到圖片類型,并將其保存到與該文件同一目錄*/
function get_name($pic_arr)
{
//圖片類型
$pattern_type = '/(/.(jpg|bmp|jpeg|gif|png))/';

foreach($pic_arr as $pic_item){//循環(huán)取出每幅圖的地址
$num = preg_match_all($pattern_type, $pic_item, $match_type);
$pic_name = get_unique().$match_type[1][0];//改時(shí)微秒時(shí)間戳命名
//以流的形式保存圖片
$write_fd = @fopen($pic_name,"wb");
@fwrite($write_fd, CurlGet($pic_item));
@fclose($write_fd);
echo "[OK]..!";
}
return 0;
}

//通過(guò)微秒時(shí)間獲得唯一ID
function get_unique(){
list($msec, $sec) = explode(" ",microtime());
return $sec.intval($msec*1000000);
}

//抓取網(wǎng)頁(yè)內(nèi)容
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_REFERER,$url);
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;
}
?>

php技術(shù)PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 阿城市| 翁牛特旗| 临武县| 沙坪坝区| 普定县| 中方县| 临西县| 卓资县| 海城市| 江阴市| 蛟河市| 勃利县| 嘉义县| 会东县| 南平市| 石家庄市| 江陵县| 澳门| 牙克石市| 抚顺县| 元阳县| 榆树市| 甘德县| 东光县| 阳原县| 远安县| 确山县| 玉山县| 大石桥市| 宁都县| 丰城市| 赤峰市| 赤壁市| 潜江市| 开鲁县| 女性| 隆回县| 松阳县| 富民县| 聊城市| 绍兴市|