How to extract strings from webpage using Regex

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts Page 1 of 1
Contributors
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

In this tutorial I will show you how to extract text out of webpage sources in order to use them in your program..

For example: http://www.bbc.co.uk/radio1/chart/singles

We want to extract the top 40 singles from this online chart.

First we will put the source code of the page in a string:
Code: Select all
Dim Source as String = New System.Net.WebClient().DownloadString("http://www.bbc.co.uk/radio1/chart/singles") 
Note that some websites may return an error and realize that you are not an actual visitor, in that case we will need a webrequest..

Now that we have the source code of the page in a string.. we just need to use regex/getbetween/split to extract the strings we want to get..

Ctrl+U on the site in your browser and find where the text you want to get is, that will be your Regex..
Code: Select all
Dim Regex As String = "  <img src=""/radio1/images/auto/70x70/.*"" alt=""(.+)?"" class=""cover"" height=""70"" width=""70"" />" 

I just use .* as a wildcard which could be anything and put (.+)? where ever the text will be that I need to get, which will be M.Groups(1).Value
Code: Select all
For Each M As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(Source, Regex)
           MessageBox.Show(M.Groups(1).Value)
        Next 
Theres your loop and there you have it.

Regards,
Skillful
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

You might want to tell more about Regular Expressions, since this does only work on one webpage and people to don't know how to use regex won't be able to use this to get other information.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Yeah this is true.
Just wrote this while on my dad's laptop.
Will update it later :)
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
CdnReaper
Just Registered
Just Registered
Posts: 1
Joined: Wed Jun 13, 2012 4:07 pm

how can i do the same thing but have it posted to a label instead of a messagebox
4 posts Page 1 of 1
Return to “Tutorials”