【问题标题】:Loop through website links and get PDF's to my computer循环浏览网站链接并获取 PDF 到我的计算机
【发布时间】:2020-03-07 13:29:08
【问题描述】:

此话题与Loop through links and download PDF's相关

我正在尝试将我当前的 VBA 代码转换为 VBScript。我已经明白我有 删除变量类型(作为 ... Dim 语句的一部分)并使用 CreatObject 来获取这些对象,但否则一切都应该按原样移植。 DoEvents 也必须替换为 Wscript.sleep 之类的内容。

我遇到了一些问题。目前在运行 VBS 文件时,我收到一条错误消息,提示“需要对象:'MSHTML'”。指向第 65 行,我有Set hDoc = MSHTML.HTMLDocument。我试图在谷歌上搜索,但对这个没有任何帮助。

我应该如何处理这个?

DownloadFiles("https://www.nordicwater.com/products/waste-water/")

Sub DownloadFiles(p_sURL)
    Set xHttp = CreateObject("Microsoft.XMLHTTP")
    Dim xHttp 
    Dim hDoc
    Dim Anchors 
    Dim Anchor 
    Dim sPath
    Dim wholeURL

    Dim internet
    Dim internetdata
    Dim internetlink
    Dim internetinnerlink 
    Dim arrLinks 
    Dim sLink 
    Dim iLinkCount 
    Dim iCounter 
    Dim sLinks

    Set internet = CreateObject("InternetExplorer.Application")
    internet.Visible = False
    internet.navigate (p_sURL)

        Do Until internet.ReadyState = 4
        Wscript.Sleep 100
        Loop

        Set internetdata = internet.document
        Set internetlink = internetdata.getElementsByTagName("a")

        i = 1

        For Each internetinnerlink In internetlink
            If Left(internetinnerlink, 36) = "https://www.nordicwater.com/product/" Then

                If sLinks <> "" Then sLinks = sLinks & vbCrLf
                sLinks = sLinks & internetinnerlink.href
                i = i + 1

            Else
            End If

    Next

    wholeURL = "https://www.nordicwater.com/"
    sPath = "C:\temp\"

    arrLinks = Split(sLinks, vbCrLf)
    iLinkCount = UBound(arrLinks) + 1

    For iCounter = 1 To iLinkCount
    sLink = arrLinks(iCounter - 1)
        'Get the directory listing
        xHttp.Open "GET", sLink
        xHttp.send

        'Wait for the page to load
        Do Until xHttp.ReadyState = 4
        Wscript.Sleep 100
        Loop

        'Put the page in an HTML document
        Set hDoc = MSHTML.HTMLDocument
        hDoc.body.innerHTML = xHttp.responseText

        'Loop through the hyperlinks on the directory listing
        Set Anchors = hDoc.getElementsByTagName("a")

        For Each Anchor In Anchors

            'test the pathname to see if it matches your pattern
            If Anchor.pathname Like "*.pdf" Then

                xHttp.Open "GET", wholeURL & Anchor.pathname, False
                xHttp.send

                With CreateObject("Adodb.Stream")
                    .Type = 1
                    .Open
                    .write xHttp.responseBody
                    .SaveToFile sPath & getName(wholeURL & Anchor.pathname), 2 '//overwrite
                End With

            End If

        Next

    Next

End Sub

功能:

Function getName(pf)
    getName = Split(pf, "/")(UBound(Split(pf, "/")))
End Function

【问题讨论】:

  • 请不要更改原始发布的代码,因为它会更改问题的上下文和任何相关的答案。最初的问题是Set hDoc = MSHTML.HTMLDocument 更改将使提供的答案无效。已将问题回滚。

标签: web-scraping vbscript web-crawler


【解决方案1】:

代替Set hDoc = MSHTML.HTMLDocument,使用:

Set hDoc = CreateObject("htmlfile")

在 VBA/VB6 中,您可以指定变量和对象类型,但不能使用 VBScript。您必须使用CreateObject(或GetObjectGetObject function)来实例化对象,如MSHTML.HTMLDocumentMicrosoft.XMLHTTPInternetExplorer.Application 等,而不是使用Dim objIE As InternetExplorer.Application 声明这些对象。

另一个变化:

If Anchor.pathname Like "*.pdf" Then

可以写成StrComp function:

If StrComp(Right(Anchor.pathname, 4), ".pdf", vbTextCompare) = 0 Then

或使用InStr function:

If InStr(Anchor.pathname, ".pdf") > 0 Then

此外,在您的子目录的开头,您可以执行以下操作:

Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim xHttp 

您应该在分配值或对象之前声明您的变量。在 VBScript 中这是非常轻松的,您的代码可以工作,因为 VBScript 会为您创建未定义的变量,但是在使用它们之前Dim 您的变量是一个很好的做法。

除了Wscript.sleep 命令,您的 VBScript 代码将在 VB6/VBA 中运行,因此您可以在 VB6 或 VBA 应用程序(如 Excel)中调试您的脚本。

【讨论】:

  • 使用Set hDoc = CreateObject("MSHTML.HTMLDocument") 后我又遇到了一个错误。查看编辑后的帖子。
  • 抱歉,请改用CreateObject("htmlfile")
  • 是的,我已经有了这个,但是接下来的行还有一些其他问题...hDoc.body.innerHTML = xHttp.responseText
  • 根据这个stackoverflow.com/questions/9995257/…hDoc.body.innerHTML = xHttp.responseText应该可以吗?
  • 那几行很重要!!它们将网页内容加载到 HTML 文档对象中。没有这个,你的 Anchors 集合将是空的。
猜你喜欢
  • 2012-05-25
  • 1970-01-01
  • 2012-08-24
  • 2018-11-03
  • 2018-09-18
  • 2011-05-11
  • 2019-02-26
  • 2020-03-06
  • 2021-02-16
相关资源
最近更新 更多