c# - 如何以编程方式单击另一个站点上的按钮?

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

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

  1. 我如何抓取网站并找到用户名和密码文本框并用所需数据填充它们。
  2. 那我如何按下/触发登录按钮?

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

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

最佳答案

你不想那样工作。您需要模拟网站在通过 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。

关于c# - 如何以编程方式单击另一个站点上的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488039/

相关文章:

c# - 跨本地数据库使用 INSERT INTO

c# - 是否有相当于散装的东西?

python - 抓取网站数据后数据不完整

Python 3 网站在使用用户代理欺骗时检测抓取工具

c# - 用于呈现新闻和类别的 Razor ,多个模型

c# - 无效操作异常 : No method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied arguments

c# - 执行标量();用 scope_identity() 生成 "System.InvalidCastException: Specified cast is not valid"

asp.net - 在 ASP.NET 应用程序中安全地存储连接字符串

asp.net - 使用 css 定义样式的概率

python - 需要使用python清理网络抓取的数据