Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
Por kiko
Posts
#58614
Boa tarde à todos, alguém saberia como poderia capturar o valor de "2 h 21 min" em um elemento que fiz Copy Element me deu essa string <span jstcache="265">2 h 21 min</span> em um determinado site.
Desde já agradeço e fico no aguardo
Por babdallas
#58621
Usei Expressões Regulares (REGEX).
Código: Selecionar todos
Public Function GetString(strTexto As String, strPadrao As String) As Variant
    Dim objRegExp       As Object
    Dim vrtMatches      As Variant
    Dim vrtMatch        As Variant
    Dim vrtDados()      As Variant
    Dim lngCont         As Long
    
    Set objRegExp = VBA.CreateObject("VBScript.RegExp")
    
    With objRegExp
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = strPadrao
        
        Set vrtMatches = .Execute(sourceString:=strTexto)
        
        ReDim vrtDados(1 To vrtMatches.Count)
        
        lngCont = 1
        For Each vrtMatch In vrtMatches
            vrtDados(lngCont) = vrtMatch.Value
            lngCont = lngCont + 1
        Next vrtMatch
        
        GetString = vrtDados
    End With
End Function

Public Sub Teste()
    VBA.MsgBox GetString("<span jstcache=""265"">2 h 21 min</span>", "\d h \d{1,2} min")(1)
End Sub
Por kiko
Posts
#58651
babdallas bom dia e muito obrigado pela atenção dispensada, mas como usaria essa Função para pegar o ELEMENTO existente no site para retornar o valor, no caso apresentado é "2 h 21 min", mas poderia ser "21 min" ou até mesmo "2 h". Essa Função respeitaria as condições apresentadas anteriormente?
Como usaria com XMLHttpRequest As Object?
Como você percebeu não é somente um dado coletado, mas são vários na mesma solicitação.
Desde já agradeço e fico no aguardo.
Por babdallas
#58653
Eu tinha entendido que você tinha conseguido colocar a parte do HTML em uma string. Você conseguiu do response text do HTML para uma variável string ou não?
Se conseguiu, basta adaptar o padrão do REGEX para \d{0,2}( h ){0,1}\d{0,2}( min){0,1} na chamada da função. Eu testei e retornou o que queria usando a função que mandei na postagem anterior. Seguem alguns exemplos:
Código: Selecionar todos
GetString("<span jstcache=""265"">2 h 21 min</span>", "\d{0,2}( h ){0,1}\d{0,2}( min){0,1}")(1)
Código: Selecionar todos
GetString("<span jstcache=""265"">2 h</span>", "\d{0,2}( h ){0,1}\d{0,2}( min){0,1}")(1)
Código: Selecionar todos
GetString("<span jstcache=""265"">23 h</span>", "\d{0,2}( h ){0,1}\d{0,2}( min){0,1}")(1)
Código: Selecionar todos
GetString("<span jstcache=""265"">21 min</span>", "\d{0,2}( h ){0,1}\d{0,2}( min){0,1}")(1)
Por kiko
Posts
#58674
babdallas boa tarde, mas aqui não deu.
Deu erro nessa linha ReDim vrtDados(1 To vrtMatches.Count)
fora do intervalo.
fico no aguardo. mais uma vez obrigado pela atenção
Por babdallas
#58677
Código: Selecionar todos
Option Explicit

Public Function GetString(strTexto As String, strPadrao As String) As String
    Dim objRegExp       As Object
    Dim vrtMatches      As Variant
    Dim vrtMatch        As Variant
    Dim vrtDados()      As Variant
    Dim lngCont         As Long
   
    Set objRegExp = VBA.CreateObject("VBScript.RegExp")
   
    With objRegExp
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = strPadrao
       
        Set vrtMatches = .Execute(sourceString:=strTexto)
       
        ReDim vrtDados(1 To vrtMatches.Count)
       
        lngCont = 0
        For Each vrtMatch In vrtMatches
            If vrtMatch.Value <> "" Then
                lngCont = lngCont + 1
                vrtDados(lngCont) = vrtMatch.Value
            End If
        Next vrtMatch
       
       ReDim Preserve vrtDados(1 To lngCont)
        GetString = vrtDados(1)
    End With
End Function

Public Sub Teste()
    VBA.MsgBox GetString("<span jstcache=""265"">2 h 21 min</span>", "(\d{1,2} h {0,1}){0,1}(\d{1,2} min){0,1}")
End Sub
Você não está autorizado a ver ou baixar esse anexo.
Por kiko
Posts
#58709
babdallas bom dia e obrigado pela atenção dispensada, com as mudanças agora funciona. Vou colocar como Resolvido e fazer alguns teste, qualquer coisa voltar a solicitar sua tal preciosa atenção.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord