scraping data from website

SHARPY1

Board Regular
Joined
Oct 1, 2007
Messages
183
Hi, a few years back somebody wrote me some code for scraping football results from a site called betexplorer. macro keeps breaking and when looking at debug it is the line highlighted below (red text) that is causing the problem. Can anybody please suggest what needs changing? it's beyond my knowledge. Thanks

Set ie = New InternetExplorer
url = "BetExplorer - Football statistics - tables, statistics, Football 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 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
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Could you provide the full code?
There are two variables (cname and leg) which currently are undefined (empty), so I cannot check the URL
 
Upvote 0
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 - Football statistics - tables, statistics, Football 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 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
 
Upvote 0
basically the macro has two buttons. one you can select to scrape all results for a particular day. this works fine. the 2nd button allows you to select all results for a particular league. this is the one that is having problems. It appears to be when league name has more than two words . example "England: National League North" doesn't work. "England: Championship" works
 
Upvote 0
It appears to be when league name has more than two words . example "England: National League North" doesn't work. "England: Championship" works

In the URL it should be "/national-league-north/", i.e. spaces are replaced with hyphens.

Therefore replace:

VBA Code:
leg = LCase(leg)

with:

VBA Code:
leg = Replace(Trim(LCase(leg))," ","-")

Please use VBA code tags when posting VBA code.
 
Upvote 0
In the URL it should be "/national-league-north/", i.e. spaces are replaced with hyphens.

Therefore replace:

VBA Code:
leg = LCase(leg)

with:

VBA Code:
leg = Replace(Trim(LCase(leg))," ","-")

Please use VBA code tags when posting VBA code.
Super thanks so much, seems to work a treat. very much appreciated :-)
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
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