close

資料來源



2-6 網頁抓取與處理


本網頁根據 IE 測試,如果你不是使用 IE,可能無法正確呈現唷!


若要使用 WSH 來直接抓取網頁,可以使用 Visual Basic 6 的元件 InetCtls.Inet,如果你的機器尚未安裝此元件,可以依照下列方式來進行安裝:

  1. 從下列網址下載壓縮檔案 msinet.cab:
    http://activex.microsoft.com/controls/vb6/msinet.cab
    (近端備份:...\wsh\download\msinet.cab

  2. 對 msinet.cab 進行解壓縮,得到 MSINET.INF 和 MSINET.OCX,將這兩個檔案放到 c:\windows\system32 目錄下。

  3. 開啟 DOS 視窗,進入 c:\windows\system32 目錄,執行「regsvr32 msinet.ocx」,即可完成安裝。
安裝完成後,我們就可以使用 WSH 來直接抓取網頁,請見下列範例:


原始檔(getWebPage01.js):(灰色區域按兩下即可拷貝)
// 抓取一個網頁
inet=new ActiveXObject("InetCtls.Inet"); // 取得 Inet Control 物件
inet.Url="http://www.cs.nthu.edu.tw"; // 欲下載之網頁
inet.RequestTimeOut=60; // 設定嘗試時間
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL(); // 下載網頁
WScript.Echo(content); // 顯示網頁內容

使用通用運算式,我們可以在抓取網頁後,顯示此網頁的標題,例如:



原始檔(getWebPage02.js):(灰色區域按兩下即可拷貝)
// 抓取一個網頁,並抓出其標題
inet=new ActiveXObject("InetCtls.Inet"); // 取得 Inet Control 物件
inet.Url="http://www.cs.nthu.edu.tw"; // 欲下載之網頁
inet.RequestTimeOut=60; // 設定嘗試時間
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL(); // 下載網頁
pattern = /<title>(.*)<\/title>/i; // 定義通用表示式
title = pattern.exec(content); // 抓出標題
WScript.Echo("位於「"+inet.Url+"」的網頁的標題是「"+RegExp.$1+"」!"); // 顯示結果

我們也可以在抓取一個網頁後,立即將網頁儲存到硬碟中的某個檔案:



原始檔(getWebPage03.js):(灰色區域按兩下即可拷貝)
// 抓取一個網頁,並將其內容存入一個檔案
inet=new ActiveXObject("InetCtls.Inet"); // 取得 Inet Control 物件
inet.Url="http://www.cs.nthu.edu.tw"; // 欲下載之網頁
inet.RequestTimeOut=20; // 設定嘗試時間
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL(); // 下載網頁
// 以下將網頁內容寫入本機檔案
fso = new ActiveXObject("Scripting.FileSystemObject");
forReading=1, forWriting=2;
fileName="test.htm";
fid=fso.OpenTextFile(fileName, forWriting, true);
fid.Write(content);
fid.Close();
WScript.Echo("從「"+inet.Url+"」抓到的內容已被存入「"+fileName+"」!");

我們也可以在抓取網頁後,利用通用表示法來抓出網頁中的連結網址和相關文字(Ancher Texts):



原始檔(getWebPage04.js):(灰色區域按兩下即可拷貝)
// 抓取一個網頁,並抽取出網頁內容的所有連結(功能並不完全,可再改進!)
inet=new ActiveXObject("InetCtls.Inet"); // 取得 Inet Control 物件
inet.Url="http://www.cs.nthu.edu.tw"; // 欲下載之網頁
inet.RequestTimeOut=20; // 設定嘗試時間
WScript.Echo("Downloading \""+inet.Url+"\"...");
content = inet.OpenURL(); // 下載網頁
pattern=/<A(.*?)<\/A>/gi; // 定義通用表示式
found=content.match(pattern); // 抓出連結
pattern2=/<\s*A\s+HREF\s*=\s*"?(.*?)"?\s*>(.*?)<\s*\/\s*A\s*>/i; // 另一個通用運算式
for (i=0; i<found.length; i++){
pattern2.exec(found[i]); // 抓出連結的網址以及連結的文字
WScript.Echo(found[i]+" ===> URL="+RegExp.$1+", TEXT="+RegExp.$2);
}

在上述範例中,我們利用 WSH 來抓取清大資訊系的首頁,並使用通用表示法來抓取連結網址及連結文字,典型輸出如下:


Downloading &quot;http://www.cs.nthu.edu.tw&quot;...<br><a target="_blank">(中)</a> ===&gt; URL=2003NTHU_CS_Chinese.doc&quot; target=&quot;_blank, TEXT=(中)<br><a target="_blank">(英)</a> ===&gt; URL=2003NTHU_CS_English.doc&quot; target=&quot;_blank, TEXT=(英)<br><a href="/intro.html" rel="nofollow ugc noreferrer noopener"><img loading="lazy" src="/icon/csbuild_8.jpg" border="0" alt=""/></a> ===&gt; URL=/intro.html, TEXT=<img loading="lazy" src="/icon/csbuild_8.jpg" border="0" alt=""/><br><a>系所自我評鑑報告</a> ===&gt; URL=Grading_report.doc, TEXT=系所自我評鑑報告<br><a href="mailto:www@cs.nthu.edu.tw" rel="nofollow ugc noreferrer noopener">意 見 與 指 教</a> ===&gt; URL=mailto:www@cs.nthu.edu.tw, TEXT=意 見 與 指 教<br><a href="/webteam" rel="nofollow ugc noreferrer noopener">製 作 小 組</a> ===&gt; URL=/webteam, TEXT=製 作 小 組<br><a><font face="Arials">學 術 卓 越 & 社 區 關 懷</font> ===&gt; URL=special.html, TEXT=<font face="Arials">學 術 卓 越 & 社 區 關 懷<br>...<br></font>前面範例所用的 InetCtls.Inet 元件,比較簡單,所以無法偵測網路斷線的情況,另一個 IIS 內建的元件 WinHttp.WinHttpRequest,則可以有較多偵錯功能,以下範例可以抓取 Google 首頁:<br><p></p><br> 原始檔(<a style="COLOR:rgb(0,128,255);TEXT-DECORATION:none;" href="http://tw.blog.yahoo.com/post/example/getWebPage05.js" rel="nofollow ugc noreferrer noopener" target="_blank">getWebPage05.js</a>):<small>(灰色區域按兩下即可拷貝)</small><pre style="BACKGROUND-COLOR:rgb(216,216,216);FONT:10pt/15px 細明體;" class="code">// 抓取一個網頁並顯示其內容<br>url=&quot;http://www.google.com&quot;;<br>try {<br> WinHttpReq = new ActiveXObject(&quot;WinHttp.WinHttpRequest.5.1&quot;);<br> WinHttpReq.Open(&quot;GET&quot;, url, false);<br> WinHttpReq.Send();<br> result = WinHttpReq.ResponseText;<br>} catch (objError) {<br> result = objError+&quot;&#92;n&quot;;<br> result += (&quot;objError.number = &quot;+(objError.number &amp; 0xFFFF).toString()+&quot;&#92;n&quot;);<br> result += (&quot;objError.description = &quot;+objError.description);<br>}<br>WScript.Echo(result);</pre> <br><p>讀者可以暫停網路,再試看看上述範例,就會印出抓不到網頁的錯誤訊息了。</p><br><p>你也可以使用另一個內建元件 XMLHTTP 來抓取網頁:</p><br><p></p><br> 原始檔(<a style="COLOR:rgb(0,128,255);TEXT-DECORATION:none;" href="http://tw.blog.yahoo.com/post/example/getUrl.js" rel="nofollow ugc noreferrer noopener" target="_blank">getUrl.js</a>):<small>(灰色區域按兩下即可拷貝)</small><pre style="BACKGROUND-COLOR:rgb(216,216,216);FONT:10pt/15px 細明體;" class="code">url = &quot;http://www.google.com.tw&quot;;<br>objHttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br>objHttp.open(&quot;GET&quot;, url, false, &quot;&quot;);<br>objHttp.send();<br>content = objHttp.responseText;<br>WScript.Echo(content);</pre> <br><p>我們也可以抓取諸如 jpg 或 mid 等二進制檔案:</p><br><p></p><br> 原始檔(<a style="COLOR:rgb(0,128,255);TEXT-DECORATION:none;" href="http://tw.blog.yahoo.com/post/example/getBinaryFile.js" rel="nofollow ugc noreferrer noopener" target="_blank">getBinaryFile.js</a>):<small>(灰色區域按兩下即可拷貝)</small><pre style="BACKGROUND-COLOR:rgb(216,216,216);FONT:10pt/15px 細明體;" class="code">// Get Binary File from Web<br>// Usage: cscript getBinaryFile.js url output<br>// Example: cscript getBinaryFile.js http://neural.cs.nthu.edu.tw/jang/books/javascript/example/image/19980425/0041.jpg annie.jpg<br>// Roger Jang, 20081215, tested under Vista<br><br>args = WScript.Arguments;<br>if (args.Count()!=2) {<br> WScript.Echo(&quot;Usage: cscript &quot; + WScript.ScriptName + &quot; url output&quot;);<br> WScript.Echo(&quot;For example:&quot;);<br> WScript.Echo(&quot;&#92;tcscript &quot; + WScript.ScriptName + &quot; &#92;&quot;http://neural.cs.nthu.edu.tw/jang/books/javascript/example/image/19980425/0041.jpg&#92;&quot; &#92;&quot;annie.jpg&#92;&quot;&quot;);<br> WScript.Quit();<br>}<br><br>url = args(0);<br>output = args(1);<br>getBinaryFile(url, output);<br><br>function getBinaryFile(url, output){<br><br> // ====== 使用 Microsoft.XMLHTTP 抓取檔案。<br> http = WScript.CreateObject(&quot;Msxml2.ServerXMLHTTP.3.0&quot;);<br> http.open(&quot;GET&quot;, url, false);<br> WScript.Echo(&quot;Retrieving file at &quot; + url);<br> http.send();<br> statusCode = http.status;<br> if (statusCode != 200){ // 檔案不正常<br> WScript.Echo(&quot;Error in retrieving file at &quot; + url);<br> WScript.Echo(&quot;Status code = &quot; + statusCode);<br> WScript.Quit();<br> }<br> file = http.responseBody;<br> BinaryStream = WScript.CreateObject(&quot;ADODB.Stream&quot;);<br> BinaryStream.type = 1;<br> BinaryStream.open();<br> // ====== 儲存檔案<br> WScript.Echo(&quot;Saving file to &quot; + output + &quot;...&quot;);<br> BinaryStream.write(file);<br> BinaryStream.saveToFile(output, 2);<br> WScript.Echo(&quot;Done.&quot;);<br>}</pre> <br><p></p><br><hr><br><strong><a style="COLOR:rgb(0,128,255);TEXT-DECORATION:none;" href="http://tw.blog.yahoo.com/post/index.asp" rel="nofollow ugc noreferrer noopener">JScript 程式設計與應用:用於單機的 WSH 環境</a></strong></a>

 
arrow
arrow
    全站熱搜

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