Exam Orph
Exam Orph

Reputation: 395

Errors when using InStr() to search HTML

I'm having difficulty using InStr() for multiple line inputs when searching through HTML code - here's a simplified version of my code with comments showing errors at debug.print:

Sub TestInStr()
Dim XMLPage As New MSXML2.XMLHTTP60
Dim html

XMLPage.Open "GET", "https://www.hltv.org/stats/matches/mapstatsid/48623/masterminds-vs-riotous-raccoons", False
XMLPage.send
If XMLPage.Status <> 200 Then MsgBox XMLPage.statusText
html = XMLPage.responseText

Dim str1, str2, str3, str4 As String

'Multi Line HTML
str1 = "<div class=""bold"">Breakdown</div>" & vbCrLf & "                    </div>" & vbCrLf & "                    <div class=""match-info-row"">" & vbCrLf & "                      <div class=""right"">"
str2 = "</div>" & vbCrLf & "                      <div class=""bold"">Team rating</div>"
Debug.Print InStr(html, str1) 'Cant find, value = 0 (Multi Line HTML)
Debug.Print InStr(html, str2) 'Cant find, value = 0 (Multi Line HTML)


'Single Line HTML
str3 = "<div class=""bold"">Breakdown</div>"
str4 = "<div class=""match-info-row"">"
Debug.Print InStr(html, str3) 'Found, value = 15503 (Single Line HTML)
Debug.Print InStr(html, str4) 'Found, value = 15208 (Single Line HTML)
End Sub

Here's the source HTML code:

            <div class="bold">Breakdown</div>
            </div>
            <div class="match-info-row">
              <div class="right">0.91 : 1.27</div>
              <div class="bold">Team rating</div>

As shown, when I try to pass multiple lines of HTML into InStr(), it return's a 0, finding nothing. But if I do the same by breaking the string down into single lines and repeat - it works fine.

Is there any way in which I could pass multiple lines of text into InStr() when searching through HTML and get a return? Thanks

Upvotes: 0

Views: 161

Answers (1)

xidgel
xidgel

Reputation: 3145

You may need to use vbCr or vbLf instead of vbCrLf to match \n. You can use Asc(Mid(html_source, position, 1)) to determine what is used for a newline in the html source.

Hope that helps

Upvotes: 2

Related Questions