Recentemente ho scaricato HtmlAgilityPack ma non ho trovato alcuna vera istruzione su come usarlo. Ho tentato di ricostruire un codice basato su vari post di discussione e altre fonti. Ecco cosa ho finora:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim document As New HtmlAgilityPack.HtmlDocument
document.LoadHtml("www.reuters.com/finance/stocks/overview?symbol=GOOG")
Dim tabletag = document.DocumentNode.SelectSingleNode("//table[@class='data']/tr[1]/td[2]")
End Sub
Come puoi vedere, sto lavorando con l'HTML da www.reuters.com/finance/stocks/overview?symbol=GOOG
.
Sto cercando di estrarre il valore Beta da questa pagina. Questo valore è attualmente 1.04.
Quando eseguo il codice sopra la finestra immediata, viene visualizzato ripetuto 100 volte:
1.04
$243,156.41
328.59
--
--
Trading Report for (GOOG). A detailed report, including free correlated market analysis, and updates.
ValuEngine Detailed Valuation Report for GOOG
GOOGLE INC CL A (GOOG) 12-months forecast
GOOGLE INC CL A (GOOG) 2-weeks forecast
Google Inc: Business description, financial summary, 3yr and interim financials, key statistics/ratios and historical ratio analysis.
Voglio solo il primo numero (1.04) restituito. Che cosa sto facendo di sbagliato? Eventuali suggerimenti?
È necessario utilizzare cookie e un proxy. Il sotto funziona alla grande per me. Fatemi sapere i vostri pensieri:
Imports System.Net
Imports System.Web
Public Class Form1
Public cookies As New CookieContainer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim wreq As HttpWebRequest = WebRequest.Create("http://www.reuters.com/finance/stocks/overview?symbol=GOOG")
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)
'just for testing:
' Dim tabletag = document.DocumentNode.SelectNodes("//table")
' MsgBox(tabletag.Nodes.Count.ToString)
'returns your field
Dim tabletag2 = document.DocumentNode.SelectSingleNode("//td[@class='data']")
MsgBox(tabletag2.InnerText)
End Sub
Private Function onPreReq(req As HttpWebRequest)
req.CookieContainer = cookies
Return True
End Function
End Class