使用 C# 进行屏幕抓取 HTTPS

发布于 2024-08-13 07:01:49 字数 22 浏览 4 评论 0原文

如何使用C# 筛选HTTPS?

How to screen scrape HTTPS using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

厌味 2024-08-20 07:01:49

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

You can use System.Net.WebClient to start an HTTPS connection, and pull down the page to scrape with that.

淡水深流 2024-08-20 07:01:49

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

You can use System.Net.WebClient to grab web pages. Here is an example: http://www.codersource.net/csharp_screen_scraping.html

澉约 2024-08-20 07:01:49

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

If for some reason you're having trouble with accessing the page as a web-client or you want to make it seem like the request is from a browser, you could use the web-browser control in an app, load the page in it and use the source of the loaded content from the web-browser control.

野鹿林 2024-08-20 07:01:49

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

Here's a concrete (albeit trivial) example. You can pass a ship name to VesselFinder in the querystring, but even if it only finds one ship with that name it still shows you the search results screen with one ship. This example detects that case and takes the user straight to the tracking map for the ship.

        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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文