【问题标题】:How can I click a button on another site programmatically?如何以编程方式单击另一个站点上的按钮?
【发布时间】:2013-01-23 19:43:57
【问题描述】:

我想用我的程序(asp.net/C#)进入另一个站点。

  1. 我如何抓取网站并找到用户名和密码文本框并在其中填写所需的数据。
  2. 那么我该如何按下/触发登录按钮?

注意:我之前使用 HtmlAgilityPack 抓取网站。有没有办法使用这个 dll 或任何其他 dll?

例如 用户名是:用户 密码是:密码 登录按钮 ID 为:按 我该怎么做?

【问题讨论】:

  • 您只需发送一个专门为他们的登录页面设计的帖子请求。这就是“按钮单击”通常的全部内容。
  • 我无法理解...你能告诉我示例代码吗?
  • 进入网络爬虫并尝试通过代码驱动页面需要您了解 HTTP 协议中发生的情况。当您第一次导航到一个页面时,这是一个 GET 请求。当您单击一个按钮(或可能的其他活动)时,您的浏览器会向页面执行 POST 并发送一组参数(使用 F12 查看 chrome 中的开发人员工具下的网络选项卡,以查看当您点击提交时发送的内容)页面表单)。

标签: c# asp.net web-scraping web-crawler


【解决方案1】:

你不想那样工作。您需要模拟一旦该表单通过 Post 请求提交到服务器并传递值后站点将执行的操作。您可以通过使用 Fiddler2 并正常执行表单提交来评估网站的功能,然后总结如下:

HttpWebRequest request;
        HttpWebResponse response;
        var responseData = "";
        var strUrl = "https://auctions.godaddy.com/trpSearchResults.aspx";

        var postData = string.Format("action=review_selected_add&items={0}_B_{1}_1|&rnd={2}&JlPYXTX=347bde7", auctionRef, bid,
            randomDouble(0, 1).ToString("0.00000000000000000"));
        request = (HttpWebRequest)WebRequest.Create(strUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/x-silverlight, application/x-silverlight-2-b2, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";
        request.Headers.Add("Accept-Encoding", "deflate");
        request.Referer = "auctions.godaddy.com";
        request.Headers["my-header"] = "the-value";
        request.KeepAlive = true;
        request.CookieContainer = cookies;
        request.Timeout = Timeout.Infinite;

        var stOut = new StreamWriter(request.GetRequestStream());
        stOut.Write(postData);
        stOut.Flush();
        stOut.Close();
        stOut = null;

        response = (HttpWebResponse)request.GetResponse();
        response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
        var encoding = new System.Text.UTF8Encoding();
        var responseReader = new StreamReader(response.GetResponseStream(), encoding, true);

        encoding = new System.Text.UTF8Encoding();
        responseReader = new StreamReader(response.GetResponseStream(), encoding, true);

        responseData = responseReader.ReadToEnd();
        response.Close();
        responseReader.Close();

只需使用上述方法创建一个类来发布数据并返回 html。

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多