Question need to get first line with the lottery numbers from html text

inkedgfx

Well-known member
Joined
Sep 29, 2012
Messages
139
Location
USA
Programming Experience
Beginner
I am having a difficult time getting the most recent winning numbers from the lottery website....I have some code that will get the html from the site , the problem I am having is ....I dont know how or where to start to get the exact line of numbers I need...the line will change twice a week.....but it will always be the first line with the numbers in it..... example of the text I need to extract the numbers from below.....the first line with the date and numbers is what I am after.....

FLORIDA LOTTERY Winning Numbers History


11-JAN-2013 Page 1 of 20


Please note every effort has been made to ensure that the enclosed information is accurate; however, in the event of an error, the winning numbers in the official record of the Florida Lottery shall be controlling.




FLORIDA LOTTO


--------------------------------------------------------------------------------

------> 01/09/13 19 - 20 - 24 - 27 - 42 - 51 X4 <--------- this is the line I need.........07/18/12 1 - 3 - 7 - 23 - 33 - 44 X5
01/05/13 8 - 19 - 21 - 23 - 36 - 40 X4 07/14/12 8 - 19 - 31 - 37 - 41 - 51 X5

01/02/13 9 - 17 - 27 - 44 - 48 - 51 X2 07/11/12 16 - 19 - 26 - 31 - 39 - 50 X3
12/29/12 15 - 18 - 21 - 31 - 40 - 41 X2 07/07/12 2 - 6 - 17 - 26 - 31 - 40 X3

12/26/12 1 - 6 - 22 - 24 - 32 - 48 X5 07/04/12 7 - 11 - 21 - 28 - 43 - 46 X4

I appreciate the help.....again

InkedGFX
 
Hi,

It's good to see that you have finally understood what was needed to get this polished off. That being having a go yourself and providing a better example of what you are working with.

Now, if you look carefully at the last example that you provided and compare this with your first example you will see two things. Firstly, why I was so persistent on getting you to provide a better example and secondly why you are not pulling out all the lottery numbers correctly. Notice the difference in the strings. You have >20< etc, >1< etc and >X3< etc and the RegEx expression has not been designed to accommodate that because we did not know that.

The next thing to consider is that the returned lottery number string is now going to be variable based on the number in the string so this needs to be accommodated for too.

So here is the final solution:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  'define the regular expressions to match date and numbers
  Dim myDateMatcher As New Regex("[0-9][0-9]/[0-9][0-9]/[0-9][0-9]")
  Dim myNumberMatcher As New Regex(">[0-9][0-9]<|>[0-9]<|>X[0-9]<") 'notice the change here to accommodate the additional strings to be parsed
 
  'get the date matches and save them
  Dim myDateMatches As MatchCollection = myDateMatcher.Matches(HTMLString)
  For Each DateMatch As Match In myDateMatches
    TextBox1.Text += DateMatch.Value & vbCrLf
    'I only want the first date mat here so exit the for loop
    Exit For
  Next
 
  'split and iterate the date matches and then ignore the first element of the array
  'since this related to HTML before the lottery dates which we want to ignore
  Dim myDateSplit() As String = myDateMatcher.Split(HTMLString)
  For DateStringCount As Integer = 1 To myDateSplit.Count - 1
    Dim myNumberMatches As MatchCollection = myNumberMatcher.Matches(myDateSplit(DateStringCount))
    'get the number matchs and save them
    For Each MatchedNumber As Match In myNumberMatches
      'here we need to check the length of the string to decide what the final number should be
      Select Case MatchedNumber.Value.Length
        Case 3
          TextBox1.Text += MatchedNumber.Value.Substring(1, 1) & vbCrLf
        Case 4
          TextBox1.Text += MatchedNumber.Value.Substring(1, 2) & vbCrLf
        Case Else
          MsgBox("Error in RegEx Parsing. Variant Not Accommodated For!")
      End Select
    Next
    'again we only want the first numbers wich mateh the first date
    'so we can exit the for loop
    Exit For
  Next
End Sub


Please read the comments carefully to see what is going on and please do try and learn something from this. I will leave it to you to make the code more efficient but I have left it as I designed it so that you can see how it all works. In addition to this, if you remove both of the Exit For statements you will see that the code will return every lottery date and every lottery number in the event that you need them.

Hope that helps.

Cheers,

Ian
 
I get an error on this line "For DateStringCount As Integer = 1 To myDateSplit.Count - 1"

error says "Count isnt a member of System.Array"

not sure why....I have (I think) all the imports.

Thanks For your Help

InkedGFX
 
Hi,

After checking the documentation on MSDN and if I am understanding it correctly the Count property is part of the MSCORLIB assembly so I am guessing that you do not have a reference to this assembly? See Here:-

Array.ICollection.Count Property (System)

Regardless of whether that helps or not you can just change the line of code causing the error to use the Length property of the Array collection. i.e:-

VB.NET:
For DateStringCount As Integer = 1 To myDateSplit.Length - 1

Hope that helps.

Cheers,

Ian
 
thank you for clearing that up....I added a reference to the mscorlib.dll but still received the error....so I changed the line of code from "count" to "length"...fixed the error.....but the code still gets only 4 of the 6 numbers.......it gets the first date then 4 numbers........

01/12/13
12
24
31
46

not sure why...I doubled checked the code and it is exact to what you posted......any ideas as to what is going on?

InkedGFX
 
Must be something on your side InkedGFX, Ian's code does the job correctly for me based on the HTML sample you provided.

((Edit: I really need to stop leaving the PC midpost.. 9 times out of 10 I come back and someone else has posted ahead of me :) )) My apologies IanRyder and InkedGFX
 
here is the code I am using....the entire html source is downloaded then put into a rich text box....then the below code is fired in a button click event....

 'define the regular expressions to match date and numbers
        HTMLString = RichTextBox1.Text
        Dim Counter As Integer = 0
        Dim myDateMatcher As New Regex("[0-9][0-9]/[0-9][0-9]/[0-9][0-9]")
        Dim myNumberMatcher As New Regex(">[0-9][0-9]<")
        'get the date match and save it
        Dim myDateMatches As MatchCollection = myDateMatcher.Matches(HTMLString)
        For Each DateMatch As Match In myDateMatches
            RichTextBox1.Text = DateMatch.Value & vbCrLf
            MostRecentDate = DateMatch.ToString
            Exit For
        Next
        'split the date match and then use the sceond element of the array to match the numbers
        Dim myDateSplit() As String = myDateMatcher.Split(HTMLString)
        For DateStringCount As Integer = 1 To myDateSplit.Length - 1
            Dim myNumberMatches As MatchCollection = myNumberMatcher.Matches(myDateSplit(DateStringCount))
            'get the number matchs and save them
            For Each MatchedNumber As Match In myNumberMatches
                Select Case MatchedNumber.Value.Length
                    Case 3
                        RichTextBox1.Text += MatchedNumber.Value.Substring(1, 1) & vbCrLf
                    Case 4
                        RichTextBox1.Text += MatchedNumber.Value.Substring(1, 2) & vbCrLf
                    Case Else
                        MsgBox("Error in Parsing Data, Variant Not Accommodated For!")
                End Select
            Next
            Exit For
        Next


not sure what is going on(as usual)

InkedGFX
 
yes...I see my mistake now...I didnt copy and paste the final solution...I always rewrite the example..so I can try to learn from it......
thank you for pointing that out for me...

InkedGFX
 
ok..I think I am finally getting my head around this...I added a few things to the code that IanRyder provided me with...now I have the html being downloaded to a stream then the numbers are processed from the reader... then they are saved and the labels are loaded with the numbers...... here is the code

Private Function GetWinningNumbersFromWebSite() As String
        ' Web site for Winning Lottery Numbers
        Dim fullpath As String = "[URL]http://www.flalottery.com/exptkt/l6.htm[/URL]"
        Dim req As HttpWebRequest
        Dim res As HttpWebResponse
        Dim sr As StreamReader
        Dim strResult As String
        Dim NumListSmall As New ArrayList
        Dim NumListLarge As New ArrayList
        Dim Numbers(5) As String
        Dim CombineNums As String = ""
        Try
            ' Create request for the web stream
            req = CType(WebRequest.Create(fullpath), HttpWebRequest)
            ' Return request in a response stream
            res = CType(req.GetResponse(), HttpWebResponse)
            sr = New StreamReader(res.GetResponseStream(), Encoding.ASCII)
            'Put the results in one big string
            strResult = sr.ReadToEnd()
            Dim myDateMatcher As New Regex("[0-9][0-9]/[0-9][0-9]/[0-9][0-9]")
            Dim myNumberMatcher As New Regex(">[0-9][0-9]<|>[0-9]<|>X|[0-9]<")
            Dim myDateMatches As MatchCollection = myDateMatcher.Matches(strResult)
            For Each DateMatch As Match In myDateMatches
                MostRecentDate = DateMatch.ToString
                Exit For
            Next
            'split the date match and then use the sceond element of the array to match the numbers
            Dim myDateSplit() As String = myDateMatcher.Split(strResult)
            For DateStringCount As Integer = 1 To myDateSplit.Length - 1
                Dim myNumberMatches As MatchCollection = myNumberMatcher.Matches(myDateSplit(DateStringCount))
                'get the number matchs and save them
                For Each MatchedNumber As Match In myNumberMatches
                    Select Case MatchedNumber.Value.Length
                        Case 3 ' grab the single digit numbers
                            WinningNumSmallString += MatchedNumber.Value.Substring(1, 1) & ","
                            '  Numbers(0) = MatchedNumber.Value.Substring(1, 1)
                        Case 4 ' grab the double digit numbers
                            WinningNumLargeString += MatchedNumber.Value.Substring(1, 2) & ","
                         

                    End Select
                Next
                Exit For
            Next
            sr.Close()
           
            lblWinningNumbers.Text = lblWinningNumbers.Text & " " & MostRecentDate
          
            ' join the two number strings
            Dim NUMstr As String = String.Join(WinningNumSmallString, WinningNumLargeString)
            ' loop thru the numbers string and pull each number out  then save the numbers and fill each label with a number
            For x As Integer = 1 To NUMstr.Length - 1
                Dim num() As String
                num = NUMstr.Split(","c)
                lblNum1.Text = num(0)
                WinningNumberOne = num(0)
                lblnum2.Text = num(1)
                WinningNumberTwo = num(1)
                lblNum3.Text = num(2)
                WinningNumberThree = num(2)
                lblNum4.Text = num(3)
                WinningNumberFour = num(3)
                lblNum5.Text = num(4)
                WinningNumberFive = num(4)
                lblNum6.Text = num(5)
                WinningNumberSix = num(5)
            Next

        Catch ex As Exception
            strResult = MsgBox("Exception occured! " & ex.Message)
        End Try 'return the results
        Return strResult
    End Function 'GetWinningNumbersFromWebSite


hopefully this is acceptable or correct code.......

thank you for all your help......

InkedGFX
 
Hi,

It's good to see that you now have a working project. I have not tested the code and I could make a few comments on use of unnecessary variables but you will lean how to be more efficient with variables as you gain more experience.

The one glaringly obvious flaw, even though it works, is that For loop at the end of the routine. When you use a For loop it is so that you can interact with a variable changing its value a fixed number of times and then do something with that variable. In your case your variable is X but where do you use your variable X in your For Loop? The answer is, you don't! Therefore the For Loop is redundant and you are currently setting your Labels and TextBox's 6 times for no reason at all. These should only be set once.

Hope that helps.

Cheers,

Ian
 
ok...I see your point...I took out the un-needed variables and removed the for loop ....everything is working .......

thank you very much for all your help.....

InkedGFX
 
I am in need of some additional help with this Lottery Number Program...it seems that the Lottery Number Program was working fine until the lottery numbers have single digit numbers in the winning numbers drawing......the last few drawing only had double digit numbers...but last night , the drawing has 3 single digit numbers and the program doesnt pick those numbers out .....the error reads as follows..."Conversion from string "" to type integer is not valid"........here is the code -

Private Function GetWinningNumbersFromWebSite() As String
        ' Web site for Winning Lottery Numbers
        Dim fullpath As String = "[URL][URL]http://www.flalottery.com/exptkt/l6.htm[/URL][/URL]"
        Dim req As HttpWebRequest
        Dim res As HttpWebResponse
        Dim sr As StreamReader
        Dim strResult As String
        Dim NumListSmall As New ArrayList
        Dim NumListLarge As New ArrayList
        Dim Numbers(5) As String
        Dim CombineNums As String = ""
        Try
            ' Create request for the web stream
            req = CType(WebRequest.Create(fullpath), HttpWebRequest)
            ' Return request in a response stream
            res = CType(req.GetResponse(), HttpWebResponse)
            sr = New StreamReader(res.GetResponseStream(), Encoding.ASCII)
            'Put the results in one big string
            strResult = sr.ReadToEnd()
            Dim myDateMatcher As New Regex("[0-9][0-9]/[0-9][0-9]/[0-9][0-9]")
            Dim myNumberMatcher As New Regex(">[0-9][0-9]<|>[0-9]<|>X|[0-9]<")
            Dim myDateMatches As MatchCollection = myDateMatcher.Matches(strResult)
            For Each DateMatch As Match In myDateMatches
                MostRecentDate = DateMatch.ToString
                Exit For
            Next
            'split the date match and then use the sceond element of the array to match the numbers
            Dim myDateSplit() As String = myDateMatcher.Split(strResult)
            For DateStringCount As Integer = 1 To myDateSplit.Length - 1
                Dim myNumberMatches As MatchCollection = myNumberMatcher.Matches(myDateSplit(DateStringCount))
                'get the number matchs and save them
                For Each MatchedNumber As Match In myNumberMatches
                    Select Case MatchedNumber.Value.Length
                        Case 3 ' grab the single digit numbers
                            WinningNumSmallString += MatchedNumber.Value.Substring(1, 1) & ","
                            '  Numbers(0) = MatchedNumber.Value.Substring(1, 1)
                        Case 4 ' grab the double digit numbers
                            WinningNumLargeString += MatchedNumber.Value.Substring(1, 2) & ","
                          
 
                    End Select
                Next
                Exit For
            Next
            sr.Close()
            
            lblWinningNumbers.Text = lblWinningNumbers.Text & " " & MostRecentDate
           
            ' join the two number strings
            Dim NUMstr As String = String.Join(WinningNumSmallString, WinningNumLargeString)
            ' loop thru the numbers string and pull each number out  then save the numbers and fill each label with a number
            
                Dim num() As String
                num = NUMstr.Split(","c)
                lblNum1.Text = num(0)
                WinningNumberOne = num(0)
                lblnum2.Text = num(1)
                WinningNumberTwo = num(1)
                lblNum3.Text = num(2)
                WinningNumberThree = num(2)
                lblNum4.Text = num(3)
                WinningNumberFour = num(3)
                lblNum5.Text = num(4)
                WinningNumberFive = num(4)
                lblNum6.Text = num(5)
                WinningNumberSix = num(5)
            
 
        Catch ex As Exception
            strResult = MsgBox("Exception occured! " & ex.Message)
        End Try 'return the results
        Return strResult
    End Function 'GetWinningNumbersFromWebSite


any help would be appreciated.

InkedGFX
 
Last edited:
Back
Top