【问题标题】:Screen scraping HTTPS using C#使用 C# 抓取 HTTPS 屏幕
【发布时间】:2010-12-23 07:02:09
【问题描述】:

如何使用 C# 屏蔽抓取 HTTPS?

【问题讨论】:

  • 我正在使用 Firefox 插件“Temper Data”。它显示所有发布的数据。

标签: c# https screen-scraping html-content-extraction


【解决方案1】:

您可以使用 System.Net.WebClient 启动 HTTPS 连接,然后拉下页面进行抓取。

【讨论】:

  • 如果需要登录获取https内容?
  • 您需要确保在 WebClient 中分配一个 CookieContainer 以便 cookie 跨多个请求传递(例如登录页面,然后是内容页面)。
  • 网站正在使用 URL 重写。如何获取完整的 url?
  • 如果你在谈论服务器端 URL 重写,不知道。但是如果你在谈论 javascript,只需在代码中解析它。
【解决方案2】:

【讨论】:

    【解决方案3】:

    如果由于某种原因您在以网络客户端身份访问页面时遇到问题,或者您想让请求看起来像是来自浏览器,您可以在应用程序中使用网络浏览器控件,加载页面并使用来自网络浏览器控件的加载内容的来源。

    【讨论】:

    • 其实这不是个坏主意。
    【解决方案4】:

    您可以使用 System.Net.WebClient 来抓取网页。这是一个例子:http://www.codersource.net/csharp_screen_scraping.html

    【讨论】:

    【解决方案5】:

    这是一个具体(尽管微不足道)的示例。您可以在查询字符串中将船舶名称传递给 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;
    

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2011-01-12
      • 2013-12-31
      相关资源
      最近更新 更多