- function match_links($document) {
- preg_match_all("'<\s*a\s.*?href\s*=\s*([\"\'])?(?(1)(.*?)\\1|([^\s\>]+))[^>]*>?(.*?)'isx",$document,$links);
- while(list($key,$val) = each($links[2])) {
- if(!empty($val))
- $match['link'][] = $val;
- }
- while(list($key,$val) = each($links[3])) {
- if(!empty($val))
- $match['link'][] = $val;
- }
- while(list($key,$val) = each($links[4])) {
- if(!empty($val))
- $match['content'][] = $val;
- }
- while(list($key,$val) = each($links[0])) {
- if(!empty($val))
- $match['all'][] = $val;
- }
- return $match;
- }
複製代碼主要是正則的問題,下面給出個asp.net下的,多測試正則擷取頁面的連結正則
- public string GetHref(string HtmlCode)
- {
- string MatchVale = "";
- string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((\w|\\|\/|\.|:|-|_)+)('|""| *|>)?";
- foreach (Match m in Regex.Matches(HtmlCode, Reg))
- {
- MatchVale += (m.Value).ToLower().Replace("href=", "").Trim() + "||";
- }
- return MatchVale;
- }
複製代碼例2,php中通過Regex下載內容中的遠程圖片的函數代碼 用PHPRegex判斷內容中的圖片,下載並儲存非本網域名稱下的圖片的程式這段程式其實是屬於“小偷程式”的重要部分。 這一段程式只是下載遠程圖片的那一段。
- if (preg_match_all("/http://[^ "']+[.jpg|.gif|.jpeg|.png]+/ui",stripcslashes($content),$aliurl)){
- $i=0; //多個檔案++
- while(list($key ,$v) = each($aliurl[0])){
- //echo $v."
";
- $filetype = pathinfo($v, PATHINFO_EXTENSION); //擷取尾碼名
- $ff = @file_get_contents($v); //擷取2進位檔案內容
- if(!stripos($v,"jbxue.com")){//判斷是否是自己網站下的圖片
- if (!empty($ff)){ //擷取到檔案就執行下面的操作
- $dir = "upload/".date("Ymd")."/";//指定新的儲存路徑
- if (!file_exists($dir)){//判斷目錄是否存在
- @mkdir($dir,511,true); //建立多級目錄,511轉換成十進位是777具有可執行許可權
- } // bbs.it-home.org
- $nfn = $dir.date("Ymdhis").$i.".".$filetype; //構建檔案的新名字
- $nf = @fopen($nfn,"w"); //建立檔案
- fwrite($nf,$ff); //寫入檔案
- fclose($nf); //關閉檔案
- $i++; //多檔案++
- echo "";
- $content = str_replace($v,$nfn, $content);//替換content中的參數
- }else{//擷取不到圖片則替換為預設圖片
- $content = str_replace($v,"/upload/201204/20120417213810742.gif", $content);//替換content中的參數
- }
- }
- }
- }
複製代碼例3,PHP通過Regex下載圖片到本地。
/*
- shortage: 如果網頁中的圖片路徑不是絕對路徑,就無法抓取
- */
- set_time_limit(0);//抓取不受時間限制
$URL='http://pp.baidu.com/';//任意網址
get_pic($URL);
function get_pic($pic_url) {
- //擷取圖片二進位流
- $data=CurlGet($pic_url);
- /*利用Regex得到圖片連結*/
- $pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/';
- $num = preg_match_all($pattern_src, $data, $match_src);
- $arr_src=$match_src[1];//獲得圖片數組
- 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);
- $pic_name = get_unique().$match_type[1][0];//改時微秒時間戳記命名
- //以流的形式儲存圖片
- $write_fd = @fopen($pic_name,"wb");
- @fwrite($write_fd, CurlGet($pic_item));
- @fclose($write_fd);
- echo "[OK]..!";
- }
- return 0;
- }
//通過微秒時間獲得唯一ID
- 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_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;
- }
- ?>
複製代碼 |