例子1:
?php include_once( curl.php /*這個(gè)文件要自己配置好*/ header( content-type:text/html;charset=utf8 $pattern_title = / title (.+) //title / //標(biāo)題匹配 $pattern_code = / tr td p (.+) //p script/ //網(wǎng)頁內(nèi)容匹配 $url= http://www.freewl.com/freezcm/2011/0413/3185.html //根網(wǎng)址 $DATA_CONTENT = CurlGet($url); $num=preg_match_all($pattern_title, $DATA_CONTENT, $match_title); for($i=0;$i $num;$i++){ echo strlen($match_title[1][$i]). -- .$match_title[1][$i]. br / echo hr / /*$trans = array( = , , br / = 。 $TRANS_CONTENT = strtr($DATA_CONTENT, $trans); echo $TRANS_CONTENT;$DATA_CONTENT=preg_replace( //s(?=/s)/ , , $DATA_CONTENT);//(?=pattern) 正向預(yù)查 $DATA_CONTENT=preg_replace( /[/n/r/t]/ , /r/n , $DATA_CONTENT);//回車換行 $DATA_CONTENT=preg_replace( / / , , $DATA_CONTENT);
/*利用正則表達(dá)式得到圖片鏈接$pattern_src = / img.*?/ ([^/ ]*(jpg|bmp|jpeg|gif)).*? / */$pattern_src = / img.*?src/=/ (.*/.jpg).*? / //只匹配jpg格式的圖片$num = preg_match_all($pattern_src, $data, $match_src);$arr_src=$match_src[1];//獲得圖片數(shù)組//get_name($arr_src);get_name_2($arr_src);echo End!!! br return 0;function get_pic_2($pic_url, $base_site) {//獲取圖片二進(jìn)制流$data=CurlGet($pic_url);/*利用正則表達(dá)式得到圖片鏈接*/$pattern_src = / img.*?/ ([^/ ]*jpg).*? / //只匹配jpg格式的圖片$num = preg_match_all($pattern_src, $data, $match_src);$arr_src=$match_src[1];//獲得圖片數(shù)組$arr_src=rev_site($arr_src, $base_site);get_name($arr_src);echo End!!! br return 0;/* 將圖片相對(duì)地址改為絕對(duì)地址 */function rev_site($site_list, $base_site){ foreach($site_list as $site_item) { if (preg_match( /^http/ , $site_item)) { $return_list[] = $site_item; }else{ $return_list[] = $base_site.$site_item; return $return_list;/*得到圖片類型,并將其保存到與該文件同一目錄*/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;function get_name_2($pic_arr) //圖片編號(hào)和類型$pattern_type = /.*//(.*?)$/ foreach($pic_arr as $pic_item){//循環(huán)取出每幅圖的地址 $num = preg_match_all($pattern_type,$pic_item,$match_type); //以流的形式保存圖片 $write_fd = @fopen($match_type[1][0], wb @fwrite($write_fd, CurlGet($pic_item)); @fclose($write_fd); echo OK.. return 0;//通過微秒時(shí)間獲得唯一IDfunction get_unique(){list($msec, $sec) = explode( ,microtime());return $sec.intval($msec*1000000);//抓取網(wǎng)頁內(nèi)容function CurlGet($url){ $url=str_replace( amp; , ,$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;?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP !
相關(guān)推薦:
php打包zip圖片下載的方法
php web請(qǐng)求安全處理
以上就是php 抓取網(wǎng)頁內(nèi)容與圖片的方法的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選