HtmlAgilityPack错误:对象引用未设置为对象的实例

时间:2013-10-24 14:55:28

标签: vb.net html-parsing html-agility-pack selectsinglenode

我几个月来一直在使用这个代码,它一直在为我工作,但现在我收到此错误:Object reference not set to an instance of an object.在这行代码中:

Dim Page_Most_Recent_Quarter As Date = document.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table//tr[2]/th[3]").InnerText

以下是完整代码:

Dim wreq As HttpWebRequest = WebRequest.Create("http://www.nasdaq.com/symbol/GOOG/financials?query=income-statement&data=quarterly")
    wreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
    wreq.Method = "get"
    Dim prox As IWebProxy = wreq.Proxy
    prox.Credentials = CredentialCache.DefaultCredentials
    Dim document As New HtmlAgilityPack.HtmlDocument
    Dim web As New HtmlAgilityPack.HtmlWeb
    web.UseCookies = True
    web.PreRequest = New HtmlAgilityPack.HtmlWeb.PreRequestHandler(AddressOf onPreReq)
    wreq.CookieContainer = cookies
    Dim res As HttpWebResponse = wreq.GetResponse()
    document.Load(res.GetResponseStream, True)
    Dim Page_Most_Recent_Quarter As Date = document.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table//tr[2]/th[3]").InnerText

我认为没有任何改变会影响这段代码。我无法弄清楚出了什么问题。

1 个答案:

答案 0 :(得分:1)

我猜这个节点不存在或已被重命名。这是一个公共网站,将对其中的内容进行更改。因为它没有找到它,所以它是一个没什么,你不能在任何事情上执行.InnerText

尝试将document.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table//tr[2]/th[3]")放入另一个变量并在调试时检查它。