首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >getElementsByClassName失败

getElementsByClassName失败
EN

Stack Overflow用户
提问于 2021-04-25 16:02:35
回答 1查看 42关注 0票数 0

我有以下代码,但它失败了,我不确定为什么?错误为"Object variable or With block variable not set“。以前用来工作的,不确定有什么变化。我已经检查了类名,但它没有在网站上更改。

代码语言:javascript
复制
Public sub tester()

Dim oHtml As HTMLDocument
Dim oElement As Object

Set oHtml = New HTMLDocument
With CreateObject("MSXML2.ServerXMLHTTP.6.0")
    .Open "GET",”https://www.cigarsofcuba.co.uk/acatalog/”, False
    .send
    oHtml.body.innerHTML = .responseText
End With
v = 0
log = "section-list"

Set dados = oHtml.getElementsByClassName(log)(v).getElementsByTagName("a")
For Each oElement In dados
    Debug.Print dados(i).href
Next oElement
End sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-25 16:17:46

以下内容将从该网页获取产品链接:

代码语言:javascript
复制
Public Sub FetchLinks()
    Const base = "https://www.cigarsofcuba.co.uk/acatalog/"
    Dim oHtml As HTMLDocument, targetLink$, I&
    
    Set oHtml = New HTMLDocument
    
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.cigarsofcuba.co.uk/acatalog/", False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36"
        .send
        oHtml.body.innerHTML = .responseText
    End With
    
    With oHtml.querySelectorAll(".section-list > li > a")
        For I = 0 To .Length - 1
            targetLink = Replace(.item(I).href, "about:", base)
            Debug.Print targetLink
        Next I
    End With
End Sub

最初的几个结果类似于:

代码语言:javascript
复制
https://www.cigarsofcuba.co.uk/acatalog/Buy-Cuban-Cigars.html
https://www.cigarsofcuba.co.uk/acatalog/Christmas-Santa-s-Grotto-p1.html
https://www.cigarsofcuba.co.uk/acatalog/New-World-Sweet---Aromatic-Cigars-p1.html
https://www.cigarsofcuba.co.uk/acatalog/Cigar-Samplers-p1.html
https://www.cigarsofcuba.co.uk/acatalog/Cigar-Connoisseurs-Collection-p1.html
https://www.cigarsofcuba.co.uk/acatalog/Cohiba-Cigars-Gift-boxes.html
https://www.cigarsofcuba.co.uk/acatalog/Romeo-Y-Julieta-Cigar-Gift-Boxes.html
https://www.cigarsofcuba.co.uk/acatalog/Montecristo-Cigar-Gift-Boxes.html
https://www.cigarsofcuba.co.uk/acatalog/Machine-Made-Packet-Cigars-p1.html
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67251086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档