[TIP] Get Images, Links, and Website Source (Browser Extras)

This will show you how to get images, links and source code from a website using a web-browser control. This can be added to an external web-browser project.

Using the code

The code requires WebBrowser, TabControl, Richtextbox, PictureBox, (2x)ListBox

Double click the WebBrowser control and in the DocumentCompleted Sub add the following code:

sourceTextBox.Text = webBrowser.DocumentText.ToString 'Gets the source of the current website loaded

For Each ele As HtmlElement In webBrowser.Document.Links
    Dim eletarget As String = ele.GetAttribute("href")
    linksListBox.Items.Add(eletarget) 'Adds the Links to the ListBox
Next

For Each ele As HtmlElement In webBrowser.Document.All
    
    If ele.GetAttribute("src").ToLower.Contains(".jpg") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        imagesListBox.Items.Add(imgsrc) 'Adds all .jpg images to the ListBox
    End If
    
    If ele.GetAttribute("src").ToLower.Contains(".png") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        imagesListBox.Items.Add(imgsrc) 'Adds all .png images to the ListBox
    End If
    
    If ele.GetAttribute("src").ToLower.Contains(".gif") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        imagesListBox.Items.Add(imgsrc) 'Adds all .gif images to the ListBox
    End If
    
    If ele.GetAttribute("src").ToLower.Contains(".bmp") Then
        Dim imgsrc As String = ele.GetAttribute("src")
        imagesListBox.Items.Add(imgsrc) 'Adds all .bmp images to the ListBox
    End If
    
Next

Now double click the ListBox which will get the images and in the Click sub add the following code:

previewPicBox.ImageLocation = imagesListBox.SelectedItem.ToString
'Gets the selected image in the ListBox and previews it in the PictureBox 

Go to the DoubleClick sub of the same ListBox as before and insert the following code:

WebBrowser.Navigate(imagesListBox.SelectedItem.ToString)
'Selected Image in ListBox navigates to the Image URL in the WebBrowser

Finally, double click the ListBox which will get the links and in the DoubleClick sub add the following code:

WebBrowser.Navigate(linksListBox.SelectedItem.ToString)
'Selected Link in ListBox navigates to the link in the WebBrowser

Overview

As you can see the code is very simple and very easy to build onto. It doesn't have to be links or images, it can also be other files and resources.

I hope you found this article interesting, if you have any problems please leave a reply. 1f609

BrowserExtras(Get link, images and source).zip 126.21KB, 3 download(s) since 2017-11-27 

   You don't have the permssion to download the attachments of this post.

Like 0 Dislike

Suggestion: please make a VB tutorial on an advanced, feature rich CEFSharp browser.

Like +1 Dislike

[notify]ShoutOut! Technology[/notify] Already working on one 1f609

Like 0 Dislike