2009-12-04 92 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;