首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >存在多个框架的Web查询

存在多个框架的Web查询
EN

Stack Overflow用户
提问于 2017-06-14 16:22:11
回答 1查看 741关注 0票数 0

我的目标是抓取网页的源代码。

该网站似乎有不同的框架,这就是为什么我的代码不能正常工作。

我试着修改了一个我在网上找到的代码,它应该可以解决框架问题。

下面的代码在以下位置创建错误(需要对象):

Set profileFrame .document.getElementById("profileFrame")

Public Sub IE_Automation()

 'Needs references to Microsoft Internet Controls and Microsoft HTML Object Library

Dim baseURL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim profileFrame As HTMLIFrame
Dim slotsDiv As HTMLDivElement

'example URL with multiple frames
baseURL = "https://www.xing.com/search/members?section=members&keywords=IT&filters%5Bcontact_level%5D=non_contact"

Set IE = New InternetExplorer
With IE
    .Visible = True

     'Navigate to the main page

    .navigate baseURL & "/publictrophy/index.htm?onlinename=ace_anubis"
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

     'Get the profileFrame iframe and navigate to it

    Set profileFrame = .document.getElementById("profileFrame")

    .navigate baseURL & profileFrame.src
    While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

    Set HTMLdoc = .document
End With

 'Display all the text in the profileFrame iframe

MsgBox HTMLdoc.body.innerText

'Display just the text in the slots_container div

Set slotsDiv = HTMLdoc.getElementById("slots_container")
MsgBox slotsDiv.innerText

End Sub
EN

回答 1

Stack Overflow用户

发布于 2017-06-17 03:30:01

嗯,我不太清楚你在这里做什么,但你能试试下面的代码吗?

Option Explicit

Sub Sample()
    Dim ie As Object
    Dim links As Variant, lnk As Variant
    Dim rowcount As Long

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate "https://www.xing.com/search/members?section=members&keywords=IT&filters%5Bcontact_level%5D=non_contact"

    'Wait for site to fully load
    'ie.Navigate2 URL
    Do While ie.Busy = True
       DoEvents
    Loop

    Set links = ie.document.getElementsByTagName("a")

    rowcount = 1

    With Sheets("Sheet1")
        For Each lnk In links
        'Debug.Print lnk.innerText
            'If lnk.classname Like "*Real Statistics Examples Part 1*" Then
                .Range("A" & rowcount) = lnk.innerText
                rowcount = rowcount + 1
                'Exit For
            'End If
        Next
    End With
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44539310

复制
相关文章

相似问题

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