【问题标题】:Automate download data from web site using vb.net使用 vb.net 从网站自动下载数据
【发布时间】:2013-08-05 19:41:03
【问题描述】:

我有一个任务,我想从某个特定网站下载数据,我检查了 httpwebrequest 和 httpwebresponse 的主题,但我仍然无法理解如何填写该网站上的文本框并按登录按钮进入网站里面,进入网站是第一步。

如果有人可以帮助我们,我想使用 VB.net 来完成这项任务。

提前致谢。

【问题讨论】:

    标签: vb.net httpwebrequest httpwebresponse


    【解决方案1】:

    我使用 ASP.NET MVC 写了一篇关于此的文章,它应该对您有所帮助:Introduction to Web Scraping with HttpWebRequest using ASP.NET MVC 3

    网页抓取意味着解析这些页面以结构化的方式提取信息片段。它还指创建一个编程接口,一个 API,它通过一个用于人类的 HTML 接口与站点交互。

    请不要以为您的程序会在各自的文本框中输入用户名和密码,然后程序会按下登录按钮让您进入网站。

    网页抓取不会以这种方式发生。

    您需要研究网站如何发布登录用户名和密码,然后使用您的代码做同样的事情来获取 cookie。在每一步,而不是在每个页面上,您都需要使用 firebug 或 chrome 开发工具查看网站是如何工作的,然后相应地发送 POST 或 GET 数据以获得您想要的。

    以下是我为从我的 WordPress 网站抓取数据而编写的内容,我在适用的地方添加了 cmets 以使代码更易于阅读。

    以下是您需要定义的常量,“UserName”和“Pwd”是我的 WordPress 帐户的登录详细信息,“Url”代表登录页面 url,“ProfileUrl”是登录页面的地址显示个人资料详情。

    const string Url = "http://yassershaikh.com/wp-login.php";  
    const string UserName = "guest";  
    const string Pwd = ".netrocks!!"; // n this not my real pwd :P  
    const string ProfileUrl = "http://yassershaikh.com/wp-admin/profile.php";  
    
    
    public ActionResult Index()  
    {  
        string postData = Crawler.PreparePostData(UserName, Pwd, Url);  
        byte[] data = Crawler.GetEncodedData(postData);
    
        string cookieValue = Crawler.GetCookie(Url, data);
    
        var model = Crawler.GetUserProfile(ProfileUrl, cookieValue);
    
        return View(model);  
    }  
    I had created a static class called “Crawler”, here’s the code for it.
    
    // preparing post data  
    public static string PreparePostData(string userName, string pwd, string url)  
    {  
        var postData = new StringBuilder();  
        postData.Append("log=" + userName);  
        postData.Append("&");  
        postData.Append("pwd=" + pwd);  
        postData.Append("&");  
        postData.Append("wp-submit=Log+In");  
        postData.Append("&");  
        postData.Append("redirect_to=" + url);  
        postData.Append("&");  
        postData.Append("testcookie=1");
    
        return postData.ToString();  
    }
    
    public static byte[] GetEncodedData(string postData)  
    {  
        var encoding = new ASCIIEncoding();  
        byte[] data = encoding.GetBytes(postData);  
        return data;  
    }
    
    public static string GetCookie(string url, byte[] data)  
    {  
        var webRequest = (HttpWebRequest)WebRequest.Create(url);  
        webRequest.Method = "POST";  
        webRequest.ContentType = "application/x-www-form-urlencoded";  
        webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2";  
        webRequest.AllowAutoRedirect = false;
    
        Stream requestStream = webRequest.GetRequestStream();  
        requestStream.Write(data, 0, data.Length);  
        requestStream.Close();
    
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
    
        string cookievalue = string.Empty;  
        if (webResponse.Headers != null && webResponse.Headers["Set-Cookie"] != null)  
        {  
            cookievalue = webResponse.Headers["Set-Cookie"];
    
            // Modify CookieValue  
            cookievalue = GenerateActualCookieValue(cookievalue);  
        }
    
        return cookievalue;  
    }
    
    public static string GenerateActualCookieValue(string cookievalue)  
    {  
        var seperators = new char[] { ';', ',' };  
        var oldCookieValues = cookievalue.Split(seperators);
    
        string newCookie = oldCookieValues[2] + ";" + oldCookieValues[0] + ";" + oldCookieValues[8] + ";" + "wp-settings-time-2=1345705901";  
        return newCookie;  
    }
    
    public static List<string> GetUserProfile(string profileUrl, string cookieValue)  
    {  
        var webRequest = (HttpWebRequest)WebRequest.Create(profileUrl);
    
        webRequest.Method = "GET";  
        webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2";  
        webRequest.AllowAutoRedirect = false;
    
        webRequest.Headers.Add("Cookie", cookieValue);
    
        var responseCsv = (HttpWebResponse)webRequest.GetResponse();  
        Stream response = responseCsv.GetResponseStream();
    
        var htmlDocument = new HtmlDocument();  
        htmlDocument.Load(response);
    
        var responseList = new List<string>();
    
        // reading all input tags in the page  
        var inputs = htmlDocument.DocumentNode.Descendants("input");
    
        foreach (var input in inputs)  
        {  
            if (input.Attributes != null)  
            {  
                if (input.Attributes["id"] != null && input.Attributes["value"] != null)  
                {  
                    responseList.Add(input.Attributes["id"].Value + " = " + input.Attributes["value"].Value);  
                }  
            }  
        }
    
        return responseList;  
    }  
    

    希望这会有所帮助。

    【讨论】:

    • 我尝试使用 fiddler,但我无法理解它的阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2011-06-23
    相关资源
    最近更新 更多