0

我正在尝试使用 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
4

1 回答 1

0

您可以使用基本的 .NET 库(HTTP 类)。我还会查看 WCF(Windows Communication Foundation),看看它是否有更简单的方法。

如果幸运的话,该站点正在使用 HTTP 基本身份验证,这很容易编程。我会检查 HTML 和 HTTP 标头以查看需要哪种身份验证方法。

于 2013-01-25T23:22:48.740 回答