Web scrapping (1 Viewer)

tatarik

Registered User.
Local time
Yesterday, 20:06
Joined
Mar 4, 2013
Messages
29
Hi All,

This is my first post on this forum :)

I've been coding this Sub in VBA in order to do web scrapping. I managed to extract the HTML code from it, that is stored in the string variable readHTML.

readHTML (the string var) looks like:
Code:
 [...] <DIV id=name class=editable-item>
<H1><SPAN class="n fn"><SPAN class=full-name>[COLOR="Red"]John Smith[/COLOR]</SPAN><SPAN></SPAN></SPAN></H1></DIV></DIV>
<DIV id=headline-container data-li-template="headline">
<DIV id=headline class=editable-item> [...]
Note: the name I want to extract is in red (John Smith).

Now I'm trying to extract a certain string (a name) from the readHTML variable, and I don't get why my code isn't working.

Code:
    Dim tempFullNameStart, tempFullNameEnd As Long
    Dim FullNameMarkerStart, FullNameMarkerEnd As String
    FullNameMarkerStart = "<SPAN class=full-name>"
    FullNameMarkerEnd = "</SPAN>"
    tempFullNameStart = InStr(1, readHTML, FullNameMarkerStart, 1) + Len(FullNameMarkerStart)
    tempFullNameEnd = InStr(tempZoneFullNameStart, readHTML, FullNameMarkerEnd, 1)
    Dim tempdifference As Long
    tempdifference = tempFullNameEnd - tempFullNameStart
    FullName = Mid(readHTML, tempFullNameStart, tempdifference)

    ' [COLOR="Red"][A][/COLOR] This is just a test to check what values the Sub is returning. 
    MsgBox ("tempFullNameStart= " & tempFullNameStart & ". tempFullNameEnd = " & tempFullNameEnd & ". Difference = " & tempdifference)
    
    MsgBox ("His name is " & FullName)

According to [A], the only issue is that tempFullNameEnd returns 0.

What is happening?

Thanks in advance,

T.
 

MarkK

bit cruncher
Local time
Yesterday, 20:06
Joined
Mar 17, 2004
Messages
8,180
It's possible you have a typo here ...
Code:
    [COLOR="Red"]tempFullNameStart[/COLOR] = InStr(1, readHTML, FullNameMarkerStart, 1) + Len(FullNameMarkerStart)
    tempFullNameEnd = InStr([COLOR="Red"]tempZoneFullNameStart[/COLOR], readHTML, FullNameMarkerEnd, 1)
Note that the identifiers in red are not the same, and probably you expect that they are. This kind of error is common if you don't Require Variable Declaration. In a VBA window, go to menu item Tools->Options->Editor tab->Code Settings section->Require Variable Declaration check box and set the check box. This adds Option Explicit to the top of your code modules and raises an error if you use variables you haven't declared.

If the setting is cleared, any undeclared identifier is implicitly and silently created as a variant variable, causing, as you now know, unexpected and tough to find errors.
 

tatarik

Registered User.
Local time
Yesterday, 20:06
Joined
Mar 4, 2013
Messages
29
Lagbolt,

This is exactly that. This was such a silly mistake, thanks a lot for pointing that out !
I just ticked the Variable Declaration check box, thanks for letting me know on how to correct this :)

Have a great day,

T.
 

Users who are viewing this thread

Top Bottom