【问题标题】:Setting focus to an IE PDF style webpage for scraping using Excel VBA将焦点设置到 IE PDF 样式网页以使用 Excel VBA 进行抓取
【发布时间】:2021-03-29 14:54:42
【问题描述】:

我浏览了这个受密码保护的网站并到达一个点,它打开了一个看起来像子窗口但有一个类似"http://yadayadayada**.pdf**?V42" 的 URL。

虽然它是 PDF,但我可以手动单击它来选择和复制数据。

我的问题是通过代码设置焦点。

我尝试了多种循环浏览打开页面的 URL 的变体,但它无法识别,我无法将焦点设置在这个上。

如果一切都失败了,我可以将其保存为 PDF,通过 Adob​​e 打开并从那里抓取。

【问题讨论】:

    标签: html css excel vba internet-explorer


    【解决方案1】:

    我不确定我是否真的明白你想要什么。以下代码测试每个打开的应用程序是否是 Internet Explorer,如果是,它是否具有正确的 url 作为位置。更多信息见 cmets。

    重要提示:无需激活选项卡即可从中获取数据。我也不知道如何在 IE 中将选项卡置于前台。不知怎的,没人知道 ;-)

    Sub ScrapeFromTabs()
    
    Dim allShell As Object
    Dim oneWindow As Object
    
      Set allShell = CreateObject("Shell.Application")
      
      'Go through all open windows
      'Each tab in IE is treated as a window by the OS.
      For Each oneWindow In allShell.Windows
        'Check if it is a window of the Internet Explorer
        If InStr(1, UCase(oneWindow.FullName), "IEXPLORE") > 0 Then
          'Check whether the relevant parts are present in the URL
          If InStr(1, oneWindow.locationURL, "yadayadayada") > 0 And InStr(1, oneWindow.locationURL, ".pdf") > 0 Then
            'Do here what you want with the pdf
            'Whatever is possible with a pdf ... somthing like this
            'set nodeHeadline = oneWindow.getElementsByTagName("h1")(0)
            
            'It was the right tab with the pdf
            'Leave the loop
            Exit For
          End If
        End If
      Next oneWindow
    End Sub
    

    【讨论】:

    • 好吧,您提供的代码仍然无法对仅复制/粘贴它的页面执行我想要的操作。复制/粘贴始终默认为原始选项卡,而不是新打开的选项卡。它所做的只是识别我需要的页面的 URL,这样我就可以保存/重新打开/复制/粘贴并获得我需要的东西。发布了常见的代码字符串(Dim objShell,IE_count,x,my_url,childPage),我尝试过但不会给我 URL。你的可以。这种工作方式非常奇怪,因为我可以手动单击页面并进行复制/粘贴。无论如何,我当然可以使用它,谢谢。
    • 再次查看您的答案,Zwenn,是的,我不应该激活页面来从中抓取数据。从我的原始选项卡中,我打开了第二个选项卡,它是 pdf 页面,看起来它是焦点,但 Ctrl-a 或 IE.execWB 17,0 从原始选项卡中选择所有内容,而不是刚刚打开的选项卡。很奇怪
    猜你喜欢
    • 2021-09-29
    • 2019-04-03
    • 2017-11-04
    • 2016-09-12
    • 1970-01-01
    • 2020-11-30
    • 2013-08-27
    • 2021-01-19
    • 2019-07-30
    相关资源
    最近更新 更多