到目前為止,我有以下代碼:

Dim wreqBalQtr As HttpWebRequest = WebRequest.Create("http://www.reuters.com/finance/stocks/incomeStatement/detail?stmtType=BAL&perType=INT&symbol=" & tickerValue)
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 TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable Financials']//tr[13]//td[3]")

HTML是:

Dim wreqBalQtr As HttpWebRequest = WebRequest.Create("http://www.reuters.com/finance/stocks/incomeStatement/detail?stmtType=BAL&perType=INT&symbol=" & tickerValue)
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 TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable Financials']//tr[13]//td[3]")

當我嘗試使用變量TotalCurrentAssets從Total Current Assets行中提取第二列時(該數字應為53,857.0)。我得到“NullReferenceException未被用戶代碼處理。對象引用未設置為對象的實例。”

這是否意味著變量TotalCurrentAssets什麼都不返回?我假設我的錯誤在我的最後一行代碼中。

附加信息

我不知道是否影響我的問題,但我在原始帖子的代碼之前運行此代碼:

Dim wreqBalQtr As HttpWebRequest = WebRequest.Create("http://www.reuters.com/finance/stocks/incomeStatement/detail?stmtType=BAL&perType=INT&symbol=" & tickerValue)
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 TotalCurrentAssets = documentBalQtr.DocumentNode.SelectSingleNode("//table[@class='dataTable Financials']//tr[13]//td[3]")

在開始解析新網頁之前,是否需要從此代碼中關閉任何內容?

一般承認的答案

屬性的名稱匹配區分大小寫。您的HTML包含*f* inancials ,您的代碼需要*F* inancials

//table[upper-case(@class)=upper-case('dataTable financials')]可以使用//table[upper-case(@class)=upper-case('dataTable financials')]來防止出現這類問題。或者您可以使用XPath 2.0 matches功能。

最重要的是,確保您訪問的網頁實際上返回您期望的數據。在我的本地測試中,返回的頁面不包含具有與您的示例類似的數據的表。

將監視放在documentBalQtr.DocumentNode.OuterHtml以驗證您的頁面是否與您期望的頁面實際匹配。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因