Innertext the "src" address using MSXML2.xmlHttp

Estevam Guilherme

New Member
Joined
Jul 16, 2015
Messages
7
I’m trying to innertext a specific "src" address using MSXML2.xmlHttp. But, when I try sending the request with xmlhttp I get no error but it also give me nothing in return. How can I solve that?

Code:
Sub WebData()
    Dim obj As Object, resp As String
    With CreateObject("MSXML2.xmlHttp")
        .Open "GET", "stURL", False
        .send
        resp = .responseText
    End With
 
    With CreateObject("HTMLFile")
        .write resp
        For Each obj In .all.getElementsByTagName("img")
            If obj.Content Like "src:/*/" Then
                MsgBox obj.Content
            End If
        Next
    End With
 
Last edited:

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,658
Hi,
You have to use real URL like "https://www.mrexcel.com/" instead of "stUrl"
For example like this:
Rich (BB code):
Sub WebData()
   
    Dim s As String, obj As Object, resp As String
   
    With CreateObject("MSXML2.xmlHttp")
        .Open "GET", "https://www.mrexcel.com/", False
        .send
        resp = .responseText
    End With
 
    With CreateObject("HTMLFile") 
        .write resp
        For Each obj In .Body.Document.getElementsByTagName("img")
           s = obj.OuterHTML
           Debug.Print s ' you may parse s for "src=*" here
        Next
    End With
   
End Sub
Regards
 
Last edited:

Estevam Guilherme

New Member
Joined
Jul 16, 2015
Messages
7
Hello Mr.!

in order to reach my goal I used a real url as you mentioned. It almost brings me all "img" addresses...except the one I want. Maybe it's because its location is between a "table".

HTML:
[TABLE]
<tbody>[TR]
[TD="class: line-content"] <table summary="Box per effettuare il login a sistema, inserire username e password." style="float: right">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <caption>Login a sistema</caption>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td scope="row">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <label for="UserName" id="UserNameLabel">Nome Utente (E-mail):</label>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input name="UserName" type="text" maxlength="255" id="UserName" accesskey="l" tabindex="1" title="Nome Utente (E-mail)" class="myInput" style="width:170px;" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td scope="row">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <label for="Password" id="PasswordLabel">Password:</label>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input name="Password" type="password" maxlength="50" id="Password" tabindex="2" title="Password" class="myInput" style="width:170px;" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <label for="loginCaptcha" id="LblCapt">Codice di verifica:</label>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td scope="row">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input name="loginCaptcha" type="text" id="loginCaptcha" tabindex="3" title="Codice di verifica:" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <img id="captchaLogin" src="[URL="https://prenotaonline.esteri.it/captcha/default.aspx?pos=2&vers=361676769"]captcha/default.aspx?pos=2&vers=361676769[/URL]" alt="Codice di verifica" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            <tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input type="submit" name="BtnPwdDimenticataL" value="Ho dimenticato la password" id="BtnPwdDimenticataL" tabindex="5" title="Ho dimenticato la password" class="linkButton" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                <td style="text-align: right">[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                    <input type="submit" name="BtnConfermaL" value="Login" id="BtnConfermaL" tabindex="4" title="Login" class="inputButton" style="width:170px;" />[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                                </td>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                            </tr>[/TD]
[/TR]
[TR]
[TD="class: line-number"][/TD]
[TD="class: line-content"]                        </table>[/TD]
[/TR]
</tbody>[/TABLE]
[HTML]
 
Last edited:

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,658
Then you may parse text of the variable resp for "src:/*/", like this:
Rich (BB code):
    ' ...
   
    Dim i As Long, j As Long
    i = InStr(resp, "src:/")
    While i > 0
      j = InStr(i + 5, resp, "/")
      If j Then
        s = Mid(resp, i, j - i + 1)
        Debug.Print s
      End If
      i = InStr(j + 1, resp, "src:/")
    Wend
 
Last edited:

Estevam Guilherme

New Member
Joined
Jul 16, 2015
Messages
7
Mr. ZVI

Thank you very mch for your support, I've been trying this for days. :)
 

Estevam Guilherme

New Member
Joined
Jul 16, 2015
Messages
7
ZVI, today I got this also working, but I'm not able to outerHTML into excel, could you please give me a light.

[Dim s As String, obj As Object, resp As String

With CreateObject("HTMLFile")
.write sResp

For Each obj In .body.document.getElementsByTagName("img")
Debug.Print obj.outerHTML
Next


End With]
 
Last edited:

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,658
Sure, try this:
Rich (BB code):
Sub WebData()
 
  Dim s As String, obj As Object, resp As String
   
  ' Put content of a web page to the resp string variable
  With CreateObject("MSXML2.xmlHttp")
    .Open "GET", "https://www.mrexcel.com/", False
    .send
    resp = .responseText
  End With
   
  ' Clear A-column
  ActiveSheet.UsedRange.EntireRow.Columns(1).ClearContents
 
  ' Put a title to A1 cell
  Range("A1").Value = "WebData"
   
  ' Write outerHTMLs below the A1 cell
  With CreateObject("HTMLFile")
    .write resp
    For Each obj In .body.document.getElementsByTagName("img")
      s = obj.outerHTML
      Range("A" & Rows.Count).End(xlUp).Offset(1).Value = s
      'Debug.Print s
    Next
  End With
 
End Sub
 
Last edited:

Estevam Guilherme

New Member
Joined
Jul 16, 2015
Messages
7
Thank you ZVI, everything is working just fine. I’m quite ok with IE automation. But I’m new with MSXML2.xmlHttp and like the way you work with.
 

Watch MrExcel Video

Forum statistics

Threads
1,096,065
Messages
5,448,184
Members
405,490
Latest member
Larry of Oz

This Week's Hot Topics

Top