在编译以下代码时,我有一个错误对象变量或未设置块变量 [英] while compiling the following code i have an error Object variable or with block variable not set

查看:20
本文介绍了在编译以下代码时,我有一个错误对象变量或未设置块变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Sub GetJobDetails()
 Const URL$ = "https://www.linkedin.com/jobs/search/?geoId=103644278&keywords=nav&location=United%20States&start="
 Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
 Dim post As Object, elem$, R&, I&: I = 0
 Do
    With IE
        .Visible = True
        .navigate URL & I
        While .Busy Or .readyState < 4: DoEvents: Wend

        On Error Resume Next
        elem = .document.getElementsByTagName("li")(0).innerText
        On Error GoTo 0

        If elem = "" Then Exit Do

        For Each post In .document.getElementsByTagName("li")
            R = R + 1: Cells(R, 1) = post.getElementsByClassName("js_focusable")(0).innerText          
Cells(R, 2) = post.getElementsByClassName("job-card-search__company-name-link")(0).innerText                   
    Cells(R, 3) = post.getElementsByClassName("job-card-search__location")(0).innerText
    Next post
    End With
    I = I + 25
    elem = ""
    Application.Wait Now + TimeValue("00:00:05")
    Loop
    IE.Quit
 End Sub

究竟是什么

对象变量或块变量未设置"

"Object variable or with block variable not set"

意思是,可能是url错误,url会像上次一样动态变化开始=25/50/75"

means,may be the url was wrong,The url will changes dynamically like in last "start =25/50/75"

推荐答案

要从下一页获取内容,您需要先登录该站点.好消息是您可以使用比 IE 快得多的 xhr 解析其登录页面的内容.

To get the content from next pages you need to log in to that site first. The good news is you can parse the content from it's landing page using xhr which is way faster than IE.

工作脚本(仅来自第一页的内容):

Working script (content from first page only):

Sub GetJobDetails()
    Const URL$ = "https://www.linkedin.com/jobs/search/?geoId=103644278&keywords=nav&location=United%20States&start=0&redirect=false&position=1&pageNum=0"
    Dim Html As New HTMLDocument, Htmldoc As New HTMLDocument
    Dim I&, R&

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
        Html.body.innerHTML = .responseText

        With Html.querySelectorAll("li.job-result-card")
            For I = 0 To .Length - 1
                Htmldoc.body.innerHTML = .Item(I).outerHTML
                R = R + 1: Cells(R, 1) = Htmldoc.querySelector(".screen-reader-text").innerText
                On Error Resume Next
                Cells(R, 2) = Htmldoc.querySelector("h4.result-card__subtitle > a").innerText
                On Error GoTo 0
                Cells(R, 3) = Htmldoc.querySelector("span.job-result-card__location").innerText
            Next I
        End With
    End With
End Sub

在执行上述脚本之前,请确保添加以下引用:

Before executing the above script make sure to add the following reference:

Microsoft HTML Object Library

再次,您需要登录该站点以解析下一页的内容.

Once again, you need to log in to that site to parse the content from next pages.

这篇关于在编译以下代码时,我有一个错误对象变量或未设置块变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆