• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • php抓取頁面與代碼解析

    發表于:2013-10-15來源:IT博客大學習作者:simondai點擊數: 標簽:php
    在做一些天氣預報或者RSS訂閱的程序時,往往需要抓取非本地文件,一般情況下都是利用php模擬瀏覽器的訪問,通過http請求訪問url地址, 然后得到html源代碼或者xml數據,得到數據我們不能直接輸出,往往需要對內容進行提取,然后再進行格式化,以更加友好的

      在做一些天氣預報或者RSS訂閱的程序時,往往需要抓取非本地文件,一般情況下都是利用php模擬瀏覽器的訪問,通過http請求訪問url地址, 然后得到html源代碼或者xml數據,得到數據我們不能直接輸出,往往需要對內容進行提取,然后再進行格式化,以更加友好的方式顯現出來。

      下面先簡單說一下本文的主要內容:

      一、 PHP抓取頁面的主要方法:

      1. file()函數 2. file_get_contents()函數 3. fopen()->fread()->fclose()模式 4.curl方式 5. fsockopen()函數 socket模式 6. 使用插件(如:http://sourceforge.net/projects/snoopy/)

      二、PHP解析html或xml代碼主要方式:

      1. 正則表達式 2. PHP DOMDocument對象 3. 插件(如:PHP Simple HTML DOM Parser)

      如果你對以上內容已經很了解,以下內容可以飄過……

      PHP抓取頁面

      1. file()函數

      

      $url='http://t.qq.com';

      $lines_array=file($url);

      $lines_string=implode('',$lines_array);

      echo htmlspecialchars($lines_string);

      ?>

      2. file_get_contents()函數

      使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設置 allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能打開遠程文件。

      

      $url='http://t.qq.com';

      $lines_string=file_get_contents($url);

      echo htmlspecialchars($lines_string);

      ?>

      3. fopen()->fread()->fclose()模式

      

      $url='http://t.qq.com';

      $handle=fopen($url,"rb");

      $lines_string="";

      do{

      $data=fread($handle,1024);

      if(strlen($data)==0){break;}

      $lines_string.=$data;

      }while(true);

      fclose($handle);

      echo htmlspecialchars($lines_string);

      ?>

      4. curl方式

      使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號去掉,而且需 要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴展。

      

      $url='http://t.qq.com';

      $ch=curl_init();

      $timeout=5;

      curl_setopt($ch, CURLOPT_URL, $url);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

      $lines_string=curl_exec($ch);

      curl_close($ch);

      echo htmlspecialchars($lines_string);

      ?>

      5. fsockopen()函數 socket模式

      socket模式能否正確執行,也跟服務器的設置有關系,具體可以通過phpinfo查看服務器開啟了哪些通信協議,比如我的本地php socket沒開啟http,只能使用udp測試一下了。

      

      $fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);

      if (!$fp) {

      echo "ERROR: $errno - $errstr
    \n";

      } else {

      fwrite($fp, "\n");

      echo fread($fp, 26);

      fclose($fp);

      }

      ?>

      6. 插件

      網上應該有比較多的插件,snoopy插件是在網上搜到的,有興趣的可以研究一下。

      PHP解析xml(html)

      1. 正則表達式:

      

      $url='http://t.qq.com';

      $lines_string=file_get_contents($url);

      eregi('',$lines_string,$title);

      echo htmlspecialchars($title[0]);

      ?>

      2. PHP DOMDocument()對象

      如果遠程的html或xml存在語法錯誤,php在解析dom的時候會報錯。

      

      $url='http://www.136web.cn';

      $html=new DOMDocument();

      $html->loadHTMLFile($url);

      $title=$html->getElementsByTagName('title');

      echo $title->item(0)->nodeValue;

      ?>

      3. 插件

      本文以PHP Simple HTML DOM Parser為例,進行簡單介紹,simple_html_dom的語法類似jQuery,它讓php操作dom,就像使用jQuery操作dom一樣的簡單。

      

      $url='http://t.qq.com';

      include_once('../simplehtmldom/simple_html_dom.php');

      $html=file_get_html($url);

      $title=$html->find('title');

      echo $title[0]->plaintext;

      ?>

      當然中國人是富有創造性的,老外往往會在技術上領先,但中國人往往會在使用上更勝一籌,往往做出一些讓老外不敢想的功能,比如php的遠程抓取與分 析,本來是為數據的整合提供方便。但國人很喜歡這個,于是乎大量的采集站,它們本身不創造任何有價值的內容,就是靠抓取別人的網站內容,并把它據為己有。 在百度里輸入“php小”關鍵詞,suggest列表第一個就是“php小偷程序”,然后把同樣的關鍵詞放入google,哥只能笑而不語。

    原文轉自:http://blogread.cn/it/article/4651

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>