Hey guys, I wrote this for Bombsaway707 a little while ago and figured I may as well share it with everyone. It basically extracts all the info about a certain player via their CBL page. It uses Regular Expressions for the most part to do all the extraction. Please make an effort to understand the code before using it. All I ask is that if you use this code, please have the consideration to credit me for it when you use it.
See notes contained within the source for what controls you need.
Here you go:
[php]
'~~~~~~CBL STAT EXTRACTOR BY JASON OF MPGH~~~~~~'
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
'\\\\\\ Any member of MPGH is entitled to \\\\\\'
'\\\\\\ make use of the following source \\\\\\'
'\\\\\\ code on the condition that credit \\\\\\'
'\\\\\\ is given to me for the original. \\\\\\'
'\\\\\\ I hope you enjoy. - Jason of MPGH \\\\\\'
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
Imports System.Text.RegularExpressions
Public Class Form1
'for this particular code, you will first need to create a DataGridView, with two "TextBoxCell" columns'
'set their "HeaderText" property to something you feel is appropriate (it makes no difference) and your'
'ready to compile!'
'create a list to store all the stats.'
Private StatList As New List(Of Stats)
'this is the RegEx pattern to extract a block of stats'
Private Const Pattern As String = _
"<tr>" & "[\s\S]" & _
" <td class=""item"">.+</td>" & "[\s\S]" & _
" <td class=""value"">.+</td>" & "[\s\S]" & _
"[\s\S]" & _
" <td class=""item"">.+</td>" & "[\s\S]" & _
" <td class=""value"">.+</td>" & "[\s\S]" & _
" </tr>"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'get the stats of the specified player.'
GetPlayerStats("JR_Pwnage")
End Sub
'the following is the sub procedure that will obtain the stats of a certain player and display them in'
'a datagridview.'
Private Sub GetPlayerStats(ByVal playerName As String)
'clear the current player'
DataGridView1. Rows.Clear()
'define pagesource'
Dim PageSource As String
'download the pagesource of the specific player'
Using wc As New Net.WebClient
PageSource = wc.DownloadString("http://player.thecaconline.net/" & playerName & ".html")
End Using
'get all the stats from the pagesource in a big block!'
Dim statCollection As MatchCollection = Regex.Matches(PageSource, Pattern)
'work through each big block and get what we want!'
For Each m As Match In statCollection
'split the block into lines, and then use the indexes to specific lines to get what we want'
Dim intoArray() As String = m.Value.Split(ChrW(10))
Dim header1 As String = Regex.Match(intoArray(1), "(?<=<td class=""item"">).+(?=</td>)").Value
Dim header2 As String = Regex.Match(intoArray(4), "(?<=<td class=""item"">).+(?=</td>)").Value
Dim content1 As String = Regex.Match(intoArray(2), "(?<=<td class=""value"">).+(?=</td>)").Value
Dim content2 As String = Regex.Match(intoArray(5), "(?<=<td class=""value"">).+(?=</td>)").Value
'this is for the equipment, which doesnt follow the exact pattern of the previous entries'
If content1.StartsWith("<a href") Then
content1 = Regex.Match(content1, "(?<=<a href="".+"" data-tooltip="".+"">).+(?=</a>)").Value
End If
If content2.StartsWith("<a href") Then
content2 = Regex.Match(content2, "(?<=<a href="".+"" data-tooltip="".+"">).+(?=</a>)").Value
End If
'add the two new stats to the statlist!'
StatList.Add(New Stats With {.Stat = header1, .Value = content1})
StatList.Add(New Stats With {.Stat = header2, .Value = content2})
Next
'add each entry to the datagridview. Alternatively you can just set the datasource of the '
'DGV to "StatList", but that means changing the property names to what you want the column'
'headers to be, so I figured this would be easier to follow'
For Each s As Stats In StatList
Dim row As New DataGridViewRow
row.Cells.AddRange(New DataGridViewTextBoxCell() {New DataGridViewTextBoxCell With {.Value = s.Stat}, New DataGridViewTextBoxCell With {.Value = s.Value}})
DataGridView1. Rows.Add(row)
Next
End Sub
End Class
'the class I created for the stats.'
Public Class Stats
Private HeaderName As String = ""
Private Contents As String = ""
'this holds the stat name (i.e Backpack A...etc)'
Public Property Stat() As String
Get
Return HeaderName
End Get
Set(ByVal value As String)
HeaderName = value
End Set
End Property
'The value of the stat above (i.e L96A1...etc)'
Public Property Value() As String
Get
Return Contents
End Get
Set(ByVal value As String)
Contents = value
End Set
End Property
End Class
[/php]
Enjoy guys, hope someone gets something out of this!
Jason
Posts 1–13 of 13 · Page 1 of 1
Post a Reply
Tags for this Thread
None
Regex
Good job Jason, thanks for sharing!
Thanks for sharing, we appreciate it
Agreed, Looks very good.
Why thank you
Yes, Regex
This is really cool...
Originally Posted by Physcadelic
This is really cool...
Cheers man
Wow! Niceeeeeeeeee
/Thanked xD
Originally Posted by AcE.bu50t
Wow! Niceeeeeeeeee
/Thanked xD
Thanks, glad you liked it.
Originally Posted by Jason
Thanks, glad you liked it.
Well, No problem, it looks so awsum!
I love the method that you use to code, it looks so advanced! SHIT!
Thank you. Epic
Just made it even more advanced but credits to you for starting me + src.
Originally Posted by UnknownCoder
Just made it even more advanced but credits to you for starting me + src.
Haha nice one, care to share what you added to make in more advanced?