Excel Macro amendment - scraping data

SHARPY1

Board Regular
Joined
Oct 1, 2007
Messages
183
Hi,
I have this macro, that somebody once did for me, for scraping results from a website called BetExplorer.
It no longer works, as i believe the website may have changed it's layout slightly. Is anybody able to adjust the macro so it works again please?
I get error message: Run-time error '91':
object variable or With block variable not set

I then go to debug and it takes me to the line in macro below that i've highlighted green.

Rich (BB code):
Public Sub newmod()
Dim i           As Long
Dim j           As Long
Dim rw          As Long
Dim cl          As Long
Dim num As Long
Dim url         As String
Dim mdate       As String
Dim leagname    As String
Dim cname As String
Dim leg As String
Dim output()    As String
Dim dt          As String
Dim ele         As Variant
Dim daterng     As Variant
Dim Doc         As HTMLDocument
Dim ie          As InternetExplorer
Dim league      As HTMLTableSection
Dim leagues     As IHTMLElementCollection
Dim leagues1     As HTMLTableSection
Dim ws          As Worksheet
Dim k As Long
Dim n As Long
mdate = InputBox("Enter League Name ." & vbCrLf & _
vbCrLf & "For ex :" & vbCrLf & vbCrLf & "ARMENIA: PREMIER-LEAGUE,BRAZIL: SERIE-B")
cname = Split(mdate, ": ")(0)
leg = Split(mdate, ": ")(1)
cname = LCase(cname)
leg = LCase(leg)
        ThisWorkbook.Worksheets.Add().Name = cname
        Set ws = ThisWorkbook.Worksheets(cname)
    On Error GoTo 0
  
    ws.UsedRange.Clear
    Set ie = New InternetExplorer
    url = "BetExplorer - Soccer statistics - tables, statistics, Soccer results, odds" & cname & "/" & leg & "/results/"
  
    With ie
        .Visible = True
        .Navigate url
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
    End With
  
    Set Doc = ie.document
    Set leagues = Doc.getElementsByClassName("table-main h-mb15 js-tablebanner-t js-tablebanner-ntb")(0).getElementsByTagName("tr")
  
    'MsgBox leagues.Length
    ReDim output(1 To leagues.Length * 15, 1 To 6)
    i = 0
    k = 0
    n = 3
    'MsgBox leagues.Length
    Dim text1 As String
  
  
    For Each league In leagues
    With league
  
  text1 = Doc.getElementsByClassName("h-text-center")(n).innerText
          
              
              
              
             
                j = 0
                k = k + 1: j = j + 1
                leagname = Doc.getElementsByClassName("in-match")(i).innerText
                On Error Resume Next
                output(k, j) = Doc.getElementsByClassName("h-text-right h-text-no-wrap")(i).innerText
                output(k, j + 1) = mdate
                output(k, j + 2) = leagname
                output(k, j + 3) = Split(output(k, j + 2), "- ")(0)
                output(k, j + 4) = Split(output(k, j + 2), "- ")(1)
             
                 If text1 = "1" Or text1 = "X" Or text1 = "2" Then
                 n = n + 3
                text1 = Doc.getElementsByClassName("h-text-center")(n).innerText
                output(k, j + 5) = text1
                n = n + 1
                Else
              
                output(k, j + 5) = text1
              
                n = n + 1
              
                End If
               'MsgBox leagname
         
           i = i + 1
          'MsgBox text1
       
       
       End With
    Next
   ie.Quit
    ws.Range("A1:F1") = Array("Date", "League", "Fixture", "Team1", "Team2", "Score")
    ws.Range("A1:F1").Interior.ThemeColor = xlThemeColorAccent1
    ws.Range("A2").Resize(UBound(output, 1), UBound(output, 2)) = output
    With ws.UsedRange
        .Columns.AutoFit
        .Borders.Weight = xlThin
        .WrapText = False
    End With
    Dim m As Long

    For m = 500 To 2 Step -1
        If IsEmpty(Range("A" & m)) Then Rows([m]).EntireRow.Delete
    Next m
    ie.Quit
End Sub
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi, Kindly share screenshot of site to show which information you are planning to extract ?
 
Upvote 0
basically, i enter the league name, and it extracts all results for the season to date. usual entry in format: ENGLAND: PREMIER-LEAGUE
with Country then semi colons then space then league name, seperated by a dash if two words. worked fine for over 2 years till last week. So i assume website has been tweaked a little. Thanks
 

Attachments

  • betex.JPG
    betex.JPG
    140.6 KB · Views: 14
Upvote 0
Hi,

Please copy below line instead of the line highlighted in green in your macro (line giving error). For ENGLAND: PREMIER-LEAGUE, sheet is showing result as attached.

VBA Code:
Set leagues = Doc.getElementsByClassName("table-main js-tablebanner-t js-tablebanner-ntb")(0).getElementsByTagName("tr")
 

Attachments

  • england.PNG
    england.PNG
    95.7 KB · Views: 38
Upvote 0
Set leagues = Doc.getElementsByClassName("table-main js-tablebanner-t js-tablebanner-ntb")(0).getElementsByTagName("tr")
Thanks so much, that's brilliant :-) so basically just remove 'h-mb15' ....what does this actually mean in laymans terms please? Thanks once again, saved me a real headache. Very much appreciated :-)
 
Upvote 0
Hi, thanks for the feedback.

Regarding your query, in web scraping we are extracting information based on Class Name/ ID available in specific tag of HTML page.

I checked the specified class name and found that it's changed in table tag. See below. So copied the class name in the macro.
VBA Code:
<table class="table-main js-tablebanner-t js-tablebanner-ntb">
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top