|
-
Jan 30th, 2013, 01:16 PM
#19
Hyperactive Member
Re: Retrieving information from website?
If you look at the comment I had at the end of that line, you'll see that webDocument needs to be created and opened as an IHTMLDocument3 interface... So here's two methods that has everything you'll need. First, there's a button click event that does the bulk of the work (you could put this into your Form.load code above, noting that I did change the last part of what to do with the result). There is also GetHTML, a function that downloads a web page and returns a string of the HTML code. The button's sub uses this GetHTML function and then creates a new HTMLDocument that incorporates the IHTMLDocument3 interface. The GetHTML function uses System.Net so you'll also have to add the line "Imports System.Net" to the top of your code. One last thing... When I ran this, I did get a pop-up about a possible security risk; I just hit continue and it worked fine. Not sure how to get around this, however, so that it acts smoother....
Here's the code:
Code:
Imports System.Net
Imports mshtml
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Cursor = Cursors.WaitCursor
Dim webElement As mshtml.IHTMLElement
Dim strURL As String = "http://vidstatsx.com/PewDiePie/videos-most-recent"
Dim strHTML As String = MiscMethods.GetHTML(strURL)
If String.IsNullOrEmpty(strHTML) Then Exit Sub
' Load HTML into parsable HTML document
Dim webDocument As mshtml.IHTMLDocument3 = New HTMLDocumentClass()
webDocument.write(strHTML)
webDocument.close()
Dim webTableCollection As mshtml.IHTMLElementCollection = webDocument.getElementsByTagName("table") ' Note, webDocument is the web page opened as a mshtml.IHTMLDocument3
For Each webElement In webTableCollection
If webElement.className = "vp_detailed" Then ' Note that the page has more with this class name, but what you are looking for is the first table with this class
Dim webTable As mshtml.HTMLTable = DirectCast(webElement, mshtml.HTMLTable)
Dim webFirstRow As mshtml.HTMLTableRow = DirectCast(webTable.rows.item(0), mshtml.HTMLTableRow) ' Gets the first row of this table
Dim webSecondCell As mshtml.HTMLTableCell = DirectCast(webFirstRow.cells.item(1), mshtml.HTMLTableCell) ' Gets the 2nd cell of the 1st row, which holds the # of Uploads. Using .item short-cut here
Dim strUploads As String = webSecondCell.innerText
MsgBox("Uploads: " & strUploads, MsgBoxStyle.Information, "Result")
Exit For
End If
Next
Me.Cursor = Cursors.Default
End Sub
Function GetHTML(ByVal pageUrl As String) As String
Dim s As String = ""
Try
Dim request As System.Net.HttpWebRequest = WebRequest.Create(pageUrl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
s = reader.ReadToEnd()
End Using
Catch ex As Exception
MsgBox("FAIL: " + ex.Message)
End Try
Return s
End Function
<edit> I almost forgot to mention that when I had tried my original code I posted, it broke at the line that begins with "Dim webSecondCell"; I guess the webFirstRow.cells doesn't have a default property so I couldn't use the shortcut... you have to spell out the full "webFirstRow.cells.item(1)". I wanted to mention that so you didn't just make the changes at the top an leave that line alone.
Last edited by Pyth007; Jan 30th, 2013 at 01:22 PM.
Reason: One last note....
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|