gpt4 book ai didi

vba - 如何从特定类中获取特定链接?

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

我想提取这个链接 从那个特定的类(class)

<tr class="even">
<td>
<a href="/italy/serie-a-2015-2016/">Serie A 2015/2016</a>
</td>

这是我写的:

Sub ExtractHrefClass()

Dim ie As Object
Dim doc As HTMLDocument
Dim class As Object
Dim href As Object

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate Range("D8")
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Set class = doc.getElementsByClassName("even")
Set href = class.getElementsByTagName("a")
Range("E8").Value = href
ie.Quit

End Sub

但不幸的是有一个错误 对象不支持此属性或方法(错误 438)在线上:

    Set href = class.getElementsByTagName("a")

更新 1

我根据@RyszardJędraszyk 的回答修改了代码,但没有输出 O_o 我在哪里做错了?

Sub ExtractHrefClass()

Dim ie As Object
Dim doc As HTMLDocument
Dim href As Object
Dim htmlEle As Object

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate Range("D8")
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE And ie.Busy = False
Set doc = ie.document
Set href = doc.getElementsByTagName("a")
For Each htmlEle In href
If htmlEle.className = "even" Then
Range("E8").Value = htmlEle
End If
Next
ie.Quit

End Sub

更新 2

正如@dee 在评论中要求的那样,有来自网页 http://www.soccer24.com/italy/serie-a/archive/ 的代码

<tbody>
<tr>
<td>
<a href="/italy/serie-a/">Serie A 2016/2017</a>
</td>
<td></td>
</tr>
<tr class="even">
<td>
<a href="/italy/serie-a-2015-2016/">Serie A 2015/2016</a>
</td>
<td>
<span class="team-logo" style="background-image: url(/res/image/data/UZbZIMhM-bsGsveSt.png)"></span><a href="/team/juventus/C06aJvIB/">Juventus</a>
</td>
</tr>
<tr>
<td>
<a href="/italy/serie-a-2014-2015/">Serie A 2014/2015</a>
</td>
<td>
<span class="team-logo" style="background-image: url(/res/image/data/UZbZIMhM-bsGsveSt.png)"></span><a href="/team/juventus/C06aJvIB/">Juventus</a>
</td>
</tr>

我只需要提取该行: /italy/serie-a-2015-2016/

最佳答案

这对我有用:

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "http://www.soccer24.com/italy/serie-a/archive/", False
.Send
MsgBox Split(Split(Split(.ResponseText, "<tr class=""even"">", 2)(1), "<a href=""", 2)(1), """", 2)(0)
End With

您需要的过程可能如下所示:
Sub ExtractHrefClass()

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", Range("D8").Value, False
.Send
Range("E8").Value = Split(Split(Split(.ResponseText, "<tr class=""even"">", 2)(1), "<a href=""", 2)(1), """", 2)(0)
End With

End Sub

关于vba - 如何从特定类中获取特定链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41523166/

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