phpRegex提取網頁超連結url與網頁中圖片

來源:互聯網
上載者:User
  1. function match_links($document) {
  2. preg_match_all("'<\s*a\s.*?href\s*=\s*([\"\'])?(?(1)(.*?)\\1|([^\s\>]+))[^>]*>?(.*?)'isx",$document,$links);
  3. while(list($key,$val) = each($links[2])) {
  4. if(!empty($val))
  5. $match['link'][] = $val;
  6. }
  7. while(list($key,$val) = each($links[3])) {
  8. if(!empty($val))
  9. $match['link'][] = $val;
  10. }
  11. while(list($key,$val) = each($links[4])) {
  12. if(!empty($val))
  13. $match['content'][] = $val;
  14. }
  15. while(list($key,$val) = each($links[0])) {
  16. if(!empty($val))
  17. $match['all'][] = $val;
  18. }
  19. return $match;
  20. }
複製代碼

主要是正則的問題,下面給出個asp.net下的,多測試正則擷取頁面的連結正則

  1. public string GetHref(string HtmlCode)
  2. {
  3. string MatchVale = "";
  4. string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((\w|\\|\/|\.|:|-|_)+)('|""| *|>)?";
  5. foreach (Match m in Regex.Matches(HtmlCode, Reg))
  6. {
  7. MatchVale += (m.Value).ToLower().Replace("href=", "").Trim() + "||";
  8. }
  9. return MatchVale;
  10. }
複製代碼

例2,php中通過Regex下載內容中的遠程圖片的函數代碼

用PHPRegex判斷內容中的圖片,下載並儲存非本網域名稱下的圖片的程式這段程式其實是屬於“小偷程式”的重要部分。

這一段程式只是下載遠程圖片的那一段。

  1. if (preg_match_all("/http://[^ "']+[.jpg|.gif|.jpeg|.png]+/ui",stripcslashes($content),$aliurl)){
  2. $i=0; //多個檔案++
  3. while(list($key ,$v) = each($aliurl[0])){
  4. //echo $v."
    ";
  5. $filetype = pathinfo($v, PATHINFO_EXTENSION); //擷取尾碼名
  6. $ff = @file_get_contents($v); //擷取2進位檔案內容
  7. if(!stripos($v,"jbxue.com")){//判斷是否是自己網站下的圖片
  8. if (!empty($ff)){ //擷取到檔案就執行下面的操作
  9. $dir = "upload/".date("Ymd")."/";//指定新的儲存路徑
  10. if (!file_exists($dir)){//判斷目錄是否存在
  11. @mkdir($dir,511,true); //建立多級目錄,511轉換成十進位是777具有可執行許可權
  12. } // bbs.it-home.org
  13. $nfn = $dir.date("Ymdhis").$i.".".$filetype; //構建檔案的新名字
  14. $nf = @fopen($nfn,"w"); //建立檔案
  15. fwrite($nf,$ff); //寫入檔案
  16. fclose($nf); //關閉檔案
  17. $i++; //多檔案++
  18. echo "";
  19. $content = str_replace($v,$nfn, $content);//替換content中的參數
  20. }else{//擷取不到圖片則替換為預設圖片
  21. $content = str_replace($v,"/upload/201204/20120417213810742.gif", $content);//替換content中的參數
  22. }
  23. }
  24. }
  25. }
複製代碼

例3,PHP通過Regex下載圖片到本地。

  1. /*

  2. shortage: 如果網頁中的圖片路徑不是絕對路徑,就無法抓取
  3. */
  4. set_time_limit(0);//抓取不受時間限制

  5. $URL='http://pp.baidu.com/';//任意網址

  6. get_pic($URL);

  7. function get_pic($pic_url) {

  8. //擷取圖片二進位流
  9. $data=CurlGet($pic_url);
  10. /*利用Regex得到圖片連結*/
  11. $pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/';
  12. $num = preg_match_all($pattern_src, $data, $match_src);
  13. $arr_src=$match_src[1];//獲得圖片數組
  14. get_name($arr_src);

  15. echo "
    finished!!!";

  16. return 0;
  17. }

  18. /*得到圖片類型,並將其儲存到與該檔案同一目錄*/

  19. function get_name($pic_arr)
  20. {
  21. //圖片類型
  22. $pattern_type = '/(/.(jpg|bmp|jpeg|gif|png))/';

  23. foreach($pic_arr as $pic_item){//迴圈取出每幅圖的地址

  24. $num = preg_match_all($pattern_type, $pic_item, $match_type);
  25. $pic_name = get_unique().$match_type[1][0];//改時微秒時間戳記命名
  26. //以流的形式儲存圖片
  27. $write_fd = @fopen($pic_name,"wb");
  28. @fwrite($write_fd, CurlGet($pic_item));
  29. @fclose($write_fd);
  30. echo "[OK]..!";
  31. }
  32. return 0;
  33. }

  34. //通過微秒時間獲得唯一ID

  35. function get_unique(){
  36. list($msec, $sec) = explode(" ",microtime());
  37. return $sec.intval($msec*1000000);
  38. }

  39. //抓取網頁內容

  40. function CurlGet($url){
  41. $url=str_replace('&','&',$url);
  42. $curl = curl_init();
  43. curl_setopt($curl, CURLOPT_URL, $url);
  44. curl_setopt($curl, CURLOPT_HEADER, false);

  45. //curl_setopt($curl, CURLOPT_REFERER,$url);

  46. curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");
  47. curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
  48. curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
  49. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  50. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
  51. $values = curl_exec($curl);
  52. curl_close($curl);
  53. return $values;
  54. }
  55. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.