gpt4 book ai didi

excel - 在 Excel VBA 中从网站问题中解析赔率

转载 作者:行者123 更新时间:2023-12-04 20:26:27 25 4
gpt4 key购买 nike

我正在尝试学习 VBA 来解析网站信息。目前我正在尝试从一个网站解析赔率,但向我显示了一个错误,我不知道我做错了什么。有人能帮我吗?

这是我的代码:

Public Sub GetOddsInfo()
Dim IE As New InternetExplorer, URL As String
Dim i As Long, ws As Worksheet
Dim headers()
Dim linkgame, linkbtts, linkover As String
Dim teams, oddHome, oddDraw, oddAway, oddBtts, oddNbtts, oddOver, oddUnder As String
Const MAX_WAIT_SEC As Long = 10
URL = "https://www.oddsportal.com/soccer/germany/2-bundesliga/results/#/page/3/"
Set ws = ThisWorkbook.Worksheets("Plan1")
headers = Array("Teams", vbNullString, "Home Odds", "Draw Odds", "Away Odds", "BTTS", _
"NBTTS", "O2.5", "U2.5")

With IE
.Visible = True
.Navigate2 URL

While .Busy Or .ReadyState < 4: DoEvents: Wend

For i = 0 To 1
'.document.getElementsByClassName(" deactivate").Length -1
.Navigate2 URL

linkgame = .document.getElementsByClassName(" deactivate")(i).getElementsByClassName("name table-participant")(0).Children(0).href
teams = .document.getElementsByClassName(" deactivate")(i).getElementsByClassName("name table-participant")(0).Children(0).innerText

.Navigate2 linkgame

oddHome = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(0).innerText
oddDraw = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(1).innerText
oddAway = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(2).innerText

linkbtts = linkgame & "#bts;2"
.Navigate2 linkbtts

oddBtts = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(0).innerText
oddNbtts = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(1).innerText

linkover = linkgame & "#over-under;2;2.50;0"
.Navigate2 linkover

oddOver = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(1).innerText
oddUnder = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(2).innerText

ws.Range("A" & i + 2) = teams
ws.Range("C" & i + 2) = oddHome
ws.Range("D" & i + 2) = oddDraw
ws.Range("E" & i + 2) = oddAway
ws.Range("F" & i + 2) = oddBtts
ws.Range("G" & i + 2) = oddNbtts
ws.Range("H" & i + 2) = oddOver
ws.Range("I" & i + 2) = oddUnder
Next i
End With
End Sub


显示 .Navigate2 linkgame 中的错误我不明白为什么自从 linkgame将是游戏的链接。

最佳答案

尝试使用 CreateObject("InternetExplorer.Application") 创建 IE 对象方法,并修改您的代码如下:

Public Sub GetOddsInfo()
Dim IE As Object, URL As String
Dim i As Long, ws As Worksheet
Dim headers()
Dim teams, oddHome, oddDraw, oddAway, oddBtts, oddNbtts, oddOver, oddUnder As String
Const MAX_WAIT_SEC As Long = 10
URL = "https://www.oddsportal.com/soccer/germany/2-bundesliga/results/#/page/3/"
Set ws = ThisWorkbook.Worksheets("Plan1")
headers = Array("Teams", vbNullString, "Home Odds", "Draw Odds", "Away Odds", "BTTS", _
"NBTTS", "O2.5", "U2.5")
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate2 URL

While .Busy Or .ReadyState < 4: DoEvents: Wend

For i = 0 To 1

Dim linkgame, linkbtts, linkover As String
'.document.getElementsByClassName("deactivate").Length -1

.Navigate2 URL
While .Busy Or .ReadyState < 4: DoEvents: Wend

linkgame = .document.getElementsByClassName("deactivate")(i).getElementsByClassName("name table-participant")(0).Children(0).href
teams = .document.getElementsByClassName("deactivate")(i).getElementsByClassName("name table-participant")(0).Children(0).innerText

.Navigate2 linkgame
While .Busy Or .ReadyState < 4: DoEvents: Wend
oddHome = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(0).innerText
oddDraw = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(1).innerText
oddAway = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(2).innerText

linkbtts = linkgame & "#bts;2"

.Navigate2 linkbtts
While .Busy Or .ReadyState < 4: DoEvents: Wend

oddBtts = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(0).innerText
oddNbtts = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(1).innerText

linkover = linkgame & "#over-under;2;2.50;0"

.Navigate2 linkover

While .Busy Or .ReadyState < 4: DoEvents: Wend

oddOver = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(1).innerText
oddUnder = .document.getElementsByClassName("aver")(0).getElementsByClassName("right")(2).innerText

ws.Range("A" & i + 2) = teams
ws.Range("C" & i + 2) = oddHome
ws.Range("D" & i + 2) = oddDraw
ws.Range("E" & i + 2) = oddAway
ws.Range("F" & i + 2) = oddBtts
ws.Range("G" & i + 2) = oddNbtts
ws.Range("H" & i + 2) = oddOver
ws.Range("I" & i + 2) = oddUnder
Next i
End With
End Sub

输出如下:

enter image description here

关于excel - 在 Excel VBA 中从网站问题中解析赔率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com