Visual Basic & HTML Agility Pack

html-agility-pack vb.net visual-studio-2012

Question

I'm attempting to get the page's title from Yahoo.

http://finance.yahoo.com/q?s=plug

I request a symbol, and it generates the following url: http://finance.yahoo.com/q?s=plug

When I load a local copy of the identical page in.html, the software runs flawlessly.

This is my code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim symbol As String, htmldoc As New HtmlDocument
    symbol = TextBox3.Text
    htmldoc.Load("http://finance.yahoo.com/q?s=plug")
    Dim items = htmldoc.DocumentNode.SelectNodes("//head/title").Select(Function(node) New KeyValuePair(Of String, String)(node.InnerText, node.InnerText))

    For Each item As KeyValuePair(Of String, String) In items
        Console.WriteLine(item.Key)
        Console.WriteLine(item.Value)
    Next

End Sub

Anyone have suggestions on how I may make this work? I want to ultimately pull stock prices, etc.

I'm also open to discovering a simpler approach to complete the task at hand. In place of KeyValuePair etc. Just something I finished working on a different SO question.

Thanks.

1
1
6/22/2013 4:17:18 AM

Popular Answer

If you want to retrieve a site address, you should use theHtmlWeb to load the docs in class. TheHtmlDocument.Load technique is limited to reading from local files (or streams). You're most likely seeing a message that says "url not supported" or "cant read from url."

Dim url = "http://finance.yahoo.com/q?s=plug"
Dim web = new HtmlWeb
Dim doc = web.Load(url)
Dim titleNode = doc.DocumentNode.SelectSingleNode("/html/head/title")
Dim title As String
If titleNode IsNot Nothing Then
    title = titleNode.InnerText
End If
3
6/21/2013 3:53:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow