vb.net で HTML Agility Pack を使用して Web ページを解析しようとしています。解析したいページを呼び出すと、ログイン ページにリダイレクトされます。私はこの Web サイトのアカウントを持っているので問題ありませんが、プログラム内からこのページにログインする方法を知る必要があります。これまでの私のコードは次のとおりです。
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