2013-01-24 436 views
0

我想在vb.net中使用HTML敏捷包解析網頁。當我打電話給我想分析的頁面時,我會重定向到一個登錄頁面。這不是一個問題,因爲我有一個這個網站的帳戶,但我需要知道如何從我的程序內登錄到此頁面。我的代碼到目前爲止如下:vb.net網站登錄

Dim logincookie As CookieContainer 
Public cookies As New CookieContainer 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    'Web Scrape Balance Sheet - Quarterly 
    Dim wreqBalQtr As HttpWebRequest = WebRequest.Create("http://www.reuters.com/finance/stocks/incomeStatement/detail?stmtType=BAL&perType=INT&symbol=goog") 
    wreqBalQtr.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5" 
    wreqBalQtr.Method = "get" 
    Dim proxBalQtr As IWebProxy = wreqBalQtr.Proxy 
    proxBalQtr.Credentials = CredentialCache.DefaultCredentials 
    Dim documentBalQtr As New HtmlAgilityPack.HtmlDocument 
    Dim webBalQtr As New HtmlAgilityPack.HtmlWeb 
    webBalQtr.UseCookies = True 
    webBalQtr.PreRequest = New HtmlAgilityPack.HtmlWeb.PreRequestHandler(AddressOf onPreReq) 
    wreqBalQtr.CookieContainer = cookies 
    Dim resBalQtr As HttpWebResponse = wreqBalQtr.GetResponse() 
    documentBalQtr.Load(resBalQtr.GetResponseStream, True) 

    Dim str As String 
    str = documentBalQtr.DocumentNode.OuterHtml 
    If str.Contains("Enter your Reuters.com account info") = True Then 

     'NEED TO LOG INTO THE PAGE THAT IS CURRENTLY PULLED UP 

     Dim TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']/tr[13]/td[3]") 
     Dim TotalCurrentLiabilities = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']//tr[29]//td[1]") 
     MsgBox(TotalCurrentAssets.InnerText) 
    Else 
     Dim TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']/tr[13]/td[3]") 
     Dim TotalCurrentLiabilities = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable financials']//tr[29]//td[1]") 
     MsgBox(TotalCurrentAssets.InnerText) 
    End If 

End Sub 

Private Function onPreReq(ByVal req As HttpWebRequest) 

    req.CookieContainer = cookies 
    Return True 

End Function 
+0

有什麼建議嗎? – gromit1

回答

0

您可能可以使用基本.NET庫(HTTP類)。我還會看看WCF(Windows Communication Foundation),看看它是否有更簡單的方法。

如果您運氣好,該網站使用HTTP基本認證,這是很容易編程。我會檢查HTML和HTTP頭以查看要調用哪種認證方法。