Visual Basic & HTML Agility Pack

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

Question

I'm trying to pull the title of page from yahoo

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

I ask user for Symbol is creates the url: http://finance.yahoo.com/q?s=plug

The program works great when I load a local .html of the same page...

Here 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 any idea on how I could get this work? I eventually want to pull stock prices etc...

I'm also down for learning an easier way to do what I am trying to accomplish. Instead of using KeyValuePair etc... Just something I finally got to work on another SO question.

Thanks.

Popular Answer

When pulling a web url, you should be using the HtmlWeb class to load the documents. The HtmlDocument.Load method can only read from local files (or streams). You're probably seeing an error along the lines of "cannot read from url" or "url not supported".

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


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why