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.
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