Pls, I want to write a vba code for web scaraping. I want to access betexplorer.com soccer section, for the matches that would be played today (if possible with option to be extend it to to other days i.e tomorrow and any other days).
The program looks at all the games being played that day. It checks each game's record to see if certain pattern happened before in previous games between those teams. For example, if Manchester United is playing against Arsenal, the program will check if both teams scored, times they scored more than 1 goal, times they scored more than 2 goals, times they scored more than 3 goals and times they scored more than 4 goals and times when both team did not score. If the program finds these things happening in at least 70% of the previous games between Manchester United and Arsenal. Then the result will be matches that has the pattern occurrence at least 70%. with the pattern that occurred listed against it and the link to the match.
@Anthony47 pls can you help. thanks for the usual help
pls anybody is free to help.
Thanks
These are the codes suggested by chatgtp. pls kindly help to modify
The second code
The program looks at all the games being played that day. It checks each game's record to see if certain pattern happened before in previous games between those teams. For example, if Manchester United is playing against Arsenal, the program will check if both teams scored, times they scored more than 1 goal, times they scored more than 2 goals, times they scored more than 3 goals and times they scored more than 4 goals and times when both team did not score. If the program finds these things happening in at least 70% of the previous games between Manchester United and Arsenal. Then the result will be matches that has the pattern occurrence at least 70%. with the pattern that occurred listed against it and the link to the match.
@Anthony47 pls can you help. thanks for the usual help
pls anybody is free to help.
Thanks
These are the codes suggested by chatgtp. pls kindly help to modify
VBA Code:
Sub CheckMatches()
Dim xmlHttp As Object
Dim htmlDoc As Object
Dim table As Object
Dim rows As Object
Dim row As Object
Dim link As Object
Dim h2hLink As String
Dim h2hDoc As Object
Dim h2hTable As Object
Dim h2hRows As Object
Dim h2hRow As Object
Dim h2hCells As Object
Dim i As Long
Dim score As String
Dim goals As Long
Dim btts As Boolean
Dim matches As Collection
' Create a new XMLHTTP object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
' Send a GET request to the website
xmlHttp.Open "GET", "https://www.betexplorer.com/soccer/", False
xmlHttp.send
' Parse the HTML response using the HTMLDocument object
Set htmlDoc = CreateObject("HTMLFile")
htmlDoc.body.innerHTML = xmlHttp.responseText
' Extract the links to the matches
Set table = htmlDoc.getElementById("sortable-1")
Set rows = table.getElementsByTagName("tr")
' Loop through the rows of the table
For Each row In rows
' Get the link to the match
Set link = row.getElementsByTagName("a")(0)
h2hLink = "https://www.betexplorer.com" & link.getAttribute("href") & "&h2h=1"
' Send a GET request to the H2H page
xmlHttp.Open "GET", h2hLink, False
xmlHttp.send
' Parse the HTML response using the HTMLDocument object
Set h2hDoc = CreateObject("HTMLFile")
h2hDoc.body.innerHTML = xmlHttp.responseText
' Extract the rows of the H2H table
Set h2hTable = h2hDoc.getElementById("h2h-matches")
Set h2hRows = h2hTable.getElementsByTagName("tr")
' Loop through the rows of the H2H table
For Each h2hRow In h2hRows
' Skip the header row
If h2hRow.className = "hl" Then
Continue For
End If
' Extract the cells of the H2H row
Set h2hCells = h2hRow.getElementsByTagName("td")
' Check if the H2H row matches the pattern
score = h2hCells(2).innerText
goals = CLng(h2hCells(3).innerText) + CLng(h2hCells(4).innerText)
btts = (h2hCells(5).innerText = "BTS")
' Update the match list if the H2H row matches the pattern
If (score = "1:1" Or score = "2:2") And goals >= 3 And btts Then
matches.Add link.getAttribute("href")
Exit For
End If
Next h2hRow
Next row
' Print the links to the matches that match the pattern
For i = 1 To matches.Count
Debug.Print matches.Item(i)
Next i
End Sub
The second code
VBA Code:
Sub CheckMatchesForPatterns()
' Set up variables
Dim url As String
Dim response As String
Dim soup As Object
Dim today_matches As Object
Dim match As Object
Dim match_link As String
Dim match_url As String
Dim h2h_stats As Variant
Dim patterns As Variant
' Set the URL to scrape
url = "https://www.betexplorer.com/soccer/"
' Create a new HTTP request and send it to the URL
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
xmlhttp.Open "GET", url, False
xmlhttp.send
' Get the response text from the request
response = xmlhttp.responseText
' Parse the HTML response text with BeautifulSoup
Set soup = CreateObject("HTMLFile")
soup.body.innerHTML = response
' Get all the matches in today's list
Set today_matches = soup.getElementById("sortable-1").getElementsByClassName("dark")
' Loop through today's matches and check h2h stats and patterns
For Each match In today_matches
' Get the match link and URL
match_link = match.getElementsByTagName("a")(0).getAttribute("href")
match_url = "https://www.betexplorer.com" & match_link
' Call the function to calculate the h2h stats
h2h_stats = GetH2HStats(match_url)
' Check if there are any h2h stats for this match
If Not IsEmpty(h2h_stats) Then
' Call the function to check for patterns in the h2h stats
patterns = CheckH2HPatterns(h2h_stats)
' Check if there are any patterns for this match
If Not IsEmpty(patterns) Then
' Print the match link and patterns to the immediate window
Debug.Print "Match: " & match_link
Debug.Print "Patterns: " & Join(patterns, ", ")
End If
End If
Next match
End Sub
Function GetH2HStats(match_url As String) As Variant
' Set up variables
Dim match_response As String
Dim match_soup As Object
Dim h2h_table As Object
Dim h2h_rows As Object
Dim total_matches As Long
Dim both_scored As Long
Dim total_over2 As Long
Dim total_under2 As Long
Dim no_score As Long
Dim row As Object
Dim columns As Object
Dim goals_home As Long
Dim goals_away As Long
Dim both_scored_pct As Double
Dim total_over2_pct As Double
Dim total_under2_pct As Double
Dim no_score_pct As Double
' Create a new HTTP request and send it to the match URL
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
xmlhttp.Open "GET", match_url, False
xmlhttp.send
' Get the response text from the request
match_response = xmlhttp.responseText
' Parse the HTML response text with BeautifulSoup
Set match_soup = CreateObject("HTMLFile")
match_soup.body.innerHTML = match_response
' Get the h2h table from the match page
Set h2h_table = match_soup.getElementsByClassName("h2h")(0).getElementsByTagName("tbody")(0)
' Get all the rows in the h2h table
Set h2h_rows = h2h_table.getElementsByTagName("tr")