我正在尝试使用 vb.net 中的 HTML Agility Pack 解析网页。当我调用要解析的页面时,我会被重定向到登录页面。这不是问题,因为我有这个网站的帐户,但我需要知道如何从我的程序中登录到这个页面。到目前为止,我的代码如下:
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