以下程式碼會介紹如何擷取遠端網頁資訊

包括 HTML tag 裡面的 Title, Description 及 Keywords:

  1. <?php
  2. //----- 定義要擷取的網頁地址
  3. $url = "http://web-address";
  4.  
  5. //----- 讀取網頁源始碼
  6. $fp = file_get_contents($url);
  7.  
  8. //----- 擷取 title 資訊
  9. preg_match("/<title>(.*)<\/title>/s", $fp, $match);
  10. $title = $match[1];
  11.  
  12. //----- 擷取 Description 及 Keywords
  13. $metatag = get_meta_tags($url);
  14. $description = $metatag["description"];
  15. $keywords = $metatag["keywords"];
  16.  
  17. //----- 印出結果
  18. echo "URL: $url\n";
  19. echo "Title: $title\n";
  20. echo "Description: $description\n";
  21. echo "Keywords: $keywords\n";
  22. ?>

原文出處:http://www.real-blog.com/programming/386 版權歸原著作者所有.

 


arrow
arrow
    全站熱搜

    Frank 發表在 痞客邦 留言(0) 人氣()