2009-12-04 81 views

回答

5

您可以使用System.Net.WebClient啓動HTTPS連接,並拉下頁面與刮。

+0

如果您需要登錄才能獲取https內容? – Oded 2009-12-04 15:35:34

+0

您需要確保在WebClient中分配CookieContainer,以便通過多個請求(例如,登錄頁面和內容頁面)傳遞cookie。 – 2009-12-04 15:38:41

+0

該網站正在使用URL重寫。我如何獲得完整的網址? – Jignesh 2009-12-04 15:48:51

2

如果您遇到訪問頁面作爲Web客戶端的麻煩某種原因,或者你想使它看起來就像請求來自瀏覽器,你可以使用網絡瀏覽器控制的應用,負載其中的頁面並使用來自Web瀏覽器控件的加載內容的來源。

+0

實際上這並不是一個壞主意。 – skimania 2012-08-21 19:32:35

4

您可以使用System.Net.WebClient來抓取網頁。這裏有一個例子:http://www.codersource.net/csharp_screen_scraping.html

+2

鏈接已關閉:我認爲這可能是更新後的鏈接 - http://www.codersource.net/microsoft-net/c-advanced/html-screen-scraping-in-c.aspx – 2010-10-20 22:03:08

0

下面是具體的(雖然簡單)的例子。您可以在querystring中將船名傳遞給VesselFinder,但即使它只能找到具有該名稱的一艘船,它仍然會顯示一艘船的搜索結果屏幕。這個例子檢測到這種情況,並將用戶直接帶到船隻的跟蹤地圖。

 string strName = "SAFMARINE MAFADI"; 
     string strURL = "https://www.vesselfinder.com/vessels?name=" + HttpUtility.UrlEncode(strName); 
     string strReturnURL = strURL; 
     string strToSearch = "/?imo="; 
     string strPage = string.Empty; 
     byte[] aReqtHTML; 


     WebClient objWebClient = new WebClient(); 
     objWebClient.Headers.Add("User-Agent: Other"); //You must do this or HTTPS won't work 
     aReqtHTML = objWebClient.DownloadData(strURL); //Do the name search 
     UTF8Encoding utf8 = new UTF8Encoding(); 

     strPage = utf8.GetString(aReqtHTML); // get the string from the bytes 

     if (strPage.IndexOf(strToSearch) != strPage.LastIndexOf(strToSearch)) 
     { 
      //more than one instance found, so leave return URL as name search 
     } 
     else if (strPage.Contains(strToSearch) == true) 
     { 
      //find the ship's IMO 
      strPage = strPage.Substring(strPage.IndexOf(strToSearch)); //cut off the stuff before 
      strPage = strPage.Substring(0, strPage.IndexOf("\"")); //cut off the stuff after 

     } 

     strReturnURL = "https://www.vesselfinder.com" + strPage;