Jump to content

Recommended Posts

Posted

Um Bem Aja a todos

Há uns tempos atrás obtive aqui no Forum este código para obter do site da santa casa os números do sorteio do Euromilhoes.

Agora com a mudança do layout do site estou  ? para obter os dados.

Aja alguém que me dê uma ajuda no que respeita a obtenção dos números, uma vez que consegui obter os valores da data do sorteio.

Aí vai o código que estava a utilizar e que agora dá o erro

"Index and length must refer to a location within the string. Parameter name: length"

na linha -  Dim num As String = n.Substring(n.IndexOf(">") + 1, 2)

Dim oWebRequest As WebRequest
        Dim oWebResponse As WebResponse = Nothing
        Dim strBuffer As String = ""
        Dim objSR As StreamReader = Nothing
        Dim leitor As Object
        Dim ler As Boolean = False
        Dim valores As String = String.Empty
        Dim i As Integer = 0

        Dim num_final As String = String.Empty
        Dim estrela_final As String = String.Empty
        

        'conecta com o website
        Try
            oWebRequest = HttpWebRequest.Create("https://www.jogossantacasa.pt/web/SCCartazResult/euroMilhoes")
            oWebResponse = oWebRequest.GetResponse()
            'Le a resposta do web site e armazena em uma stream
            objSR = New StreamReader(oWebResponse.GetResponseStream)
            strBuffer = objSR.ReadToEnd
            leitor = Split(strBuffer, vbNewLine)

            For Each linha In leitor
                If ler = True Then
                    valores += linha.ToString + vbNewLine
                    i += 1
                End If

                'If linha.Contains("Ordem Saída:") Then
                '    ler = True
                'End If

                If linha.Contains("Chave") Then
                    ler = True
                End If

                If linha.Contains("Data do Sorteio") Then
                    'Obtem o valor da linha Retira caracteres a string
                    Label17.Text = (linha.ToString.Remove(34, 7))
                End If


                If i > 8 Then
                    ler = False
                End If
            Next

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            objSR.Close()
            oWebResponse.Close()
        End Try



        Dim numeros As Object = Split(valores, vbNewLine)
        Dim estrelas As Boolean = False
        Dim num_total As Integer = 1

        For Each n As String In numeros
            If n <> String.Empty Then
                Dim num As String = n.Substring(n.IndexOf(">") + 1, 2)

                If num.Contains("<") Then
                    num = num.Remove(num.IndexOf("<"))
                End If


                If estrelas = True Then
                    If estrela_final = String.Empty Then
                        estrela_final &= num.ToString + " - "
                    Else
                        estrela_final &= num.ToString
                    End If
                Else
                    If num_total < 5 Then
                        num_final &= num.ToString + " - "
                    ElseIf num_total = 5 Then
                        num_final &= num.ToString
                    End If
                    num_total += 1
                End If

                If num = "+" Then
                    estrelas = True
                End If

            End If
        Next

        'Retira caracteres a string
        Label18.Text = Label17.Text.Remove(0, 6)



        MsgBox(Label18.Text & vbNewLine & "Números: " & num_final.ToString & vbNewLine & "Estrelas: " & estrela_final.ToString, MsgBoxStyle.Information, "Obter Chave EuroMilhões da Santa Casa")

Obrigado desde já pela atenção

Posted

Também podias usar expressões regulares, nesse caso, onde o primeiro match de uma chave (5 numeros e 2 estrelas) é a chave:

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim WC As New Net.WebClient
        Dim Rsp As String = WC.DownloadString("https://www.jogossantacasa.pt/web/SCCartazResult/euroMilhoes")
        Dim m As Match = Regex.Match(Rsp, "<li>(\d|\d\d) (\d|\d\d) (\d|\d\d) (\d|\d\d) (\d|\d\d) \+ (\d|\d\d) (\d|\d\d)</li>", RegexOptions.IgnoreCase)
        MsgBox("Chave: " & m.ToString.Replace("<li>", "").Replace("</li>", ""))
    End Sub

End Class

Sérgio Ribeiro


"Great coders aren't born. They're compiled and released"
"Expert coders do not need a keyboard. They just throw magnets at the RAM chips"

Posted

Olá Ribeiro55

Obrigado pela dica, resultou para a chave do Euromilhões

Tentei alterar o codigo para obter a chave do totoloto mas não acerto uma vez que não percebo o funcionamento da rotina.

Alterei o endereço como e evidente, e na minha ideia eliminei o ultimo valor  (\d|\d\d), mas não resultou

 Dim WC As New Net.WebClient

        Dim Rsp As String = WC.DownloadString("https://www.jogossantacasa.pt/web/SCCartazResult/totolotoNew")

     Dim m As Match = Regex.Match(Rsp, "<li>(\d|\d\d) (\d|\d\d) (\d|\d\d) (\d|\d\d) (\d|\d\d) \+ (\d|\d\d) </li>", RegexOptions.IgnoreCase)
Posted

Chamuanza eu aproveitei-me do teu código e fiz umas alterações p/ verificar a minha chave do euromilhões.

A SCML fornece um serviço de resultados via XML que te garante não teres de mudar tudo com as mudanças do layout.

https://www.jogossantacasa.pt/web/SCRss/rssFeedCartRes

Tens uma série deles e depois é só ler o XML.

Mas penso que o melhor é fazer como o Kya, está a sugerir, é o que eu vou fazer.

😛

E assim fiz:

        Dim XDOC As New Xml.XmlDocument
        XDOC.Load("https://www.jogossantacasa.pt/web/SCRss/rssFeedCartRes")
        'XDOC.Load("d:\santacasa.xml")
        Dim nodeList As XmlNodeList
        Dim root As XmlNode = XDOC.DocumentElement
        Dim chave As String = ""
        nodeList = root.SelectNodes("channel/item")

        For Each meuNode As Xml.XmlNode In nodeList
            If meuNode.SelectSingleNode("title").InnerText = "Euromilhões" Then
                chave = (meuNode.SelectSingleNode("description").InnerText)
            End If
        Next

        Dim ts As Integer = chave.Length - chave.IndexOf(":") - 1
        Dim i As Integer = chave.IndexOf(":") + 1
        Dim str As String = chave.Substring(i, ts)
        lbl_chave.Text += str
        Dim serienum As Object = Split(str, " ")

        For Each n As String In serienum
            If n <> String.Empty Then
                cadeia.Add(n.ToString)
            End If
        Next

Aqui fica a alternativa, no meu caso só quero obter a chave do euromilhões.

Obrigado Chamuanza

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.