2013-08-05 90 views
0

我有一個任務,我想從一些特定的網站下載數據,我檢查了httpwebrequest和httpwebresponse的主題,但仍然無法理解我們如何填寫該網站上的文本框並按登錄按鈕進入網站,登錄到網站是第一步。使用vb.net從網站自動下載數據

我想使用VB.net來完成這項任務,如果有人能幫助我們。

在此先感謝。

回答

0

我寫這個使用ASP.NET MVC的文章,它應該幫助你:Introduction to Web Scraping with HttpWebRequest using ASP.NET MVC 3

網絡刮痧手段解析這些網頁中提取的信息以結構化方式。它也指創建一個程序化接口,一個API,通過一個用於人類的HTML接口與一個網站進行交互。

請不要以爲您的程序會在各自的文本框中輸入用戶名和密碼,然後程序會按下登錄按鈕讓您進入網站。

Web Scraping不會以這種方式發生。

您需要研究如何在網站POST登錄用戶名和密碼,然後使用您的代碼,你需要做同樣的事情來獲取cookie。在每一步,而不是在每一頁上,您都需要使用firebug或chrome開發工具查看網站的工作方式,然後相應地發送POST或GET數據以獲得您想要的內容。

下面是我寫的從我的WordPress網站上抓取數據的地方,我在任何可以使代碼更易讀的地方添加了註釋。

以下是您需要定義的常數,'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; 
} 

希望這會有所幫助。

+0

我試圖使用提琴手,但我無法理解它的閱讀。 – user2652121