简体   繁体   English

使用VBA Excel从嵌入式网页中提取数据

[英]Pulling Data from an embedded web page with VBA Excel

I am trying to use VBA in Excel to access data in a webpage that is embedded in a webpage. 我正在尝试在Excel中使用VBA来访问嵌入在网页中的网页中的数据。 I know how to do this if the table is on a non-embedded page. 如果表格位于非嵌入式页面上,我知道该怎么做。 I also know how to navigate to this product's page using VBA. 我也知道如何使用VBA导航到该产品的页面。 I cannot just navigate to the embedded page because there is a product id look up that converts a part number to an id and i don't have access to that database. 我不能仅浏览到嵌入式页面,因为有一个产品ID查找将零件号转换为ID,而我无权访问该数据库。

Here is the link to the page: http://support.automation.siemens.com/WW/view/en/7224052 这是页面的链接: http : //support.automation.siemens.com/WW/view/zh/7224052

I would have posed a picture of the element for clarity but I don't have 10 rep points... 为了清楚起见,我会对该元素进行图片处理,但是我没有10个代表点...

The table I need to get information from is the "Product Life Cycle" table. 我需要从中获取信息的表是“产品生命周期”表。

I can see the correct url in a property called src under the corresponding item if I save the page as an HTMLDocument in VBA using the following code: 如果使用以下代码将页面另存为VBA中的HTMLDocument,则可以在相应项下的src属性中看到正确的url:

For Each cell In Selection
    link = "http://support.automation.siemens.com/US/llisapi.dll?func=cslib.csinfo&lang=en&objid=" & cell & "&caller=view"
    ie.navigate link
    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE
    Dim doc As HTMLDocument

Is there a way to index this table with VBA or will I have to contact the company and attempt to get access to the product ID so I can navigate to the page directly? 是否可以使用VBA为该表编制索引,还是必须联系公司并尝试访问产品ID,以便可以直接导航到该页面?

Regarding my comment below, here is the code that a recorded macro yeilds: 关于下面我的评论,这是录制的宏产生的代码:

ActiveCell.FormulaR1C1 = _
    "http://support.automation.siemens.com/WW/llisapi.dll?func=cslib.csinfo&lang=en&objid=6ES7194-1AA01-0XA0&caller=view"
Range("F9").Select
With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW" _
    , Destination:=Range("$F$9"))
    .FieldNames = True
    .RowNumbers = False

I know where to find the string: URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW , but I don't know how to save it to a variable. 我知道在哪里找到该字符串: URL;http://support.automation.siemens.com/WW/llisapi.dll?func=ll&objid=7224052&nodeid0=10997566&caller=view&lang=en&siteid=cseus&aktprim=0&objaction=csopen&extranet=standard&viewreg=WW ,但我不知道如何将其保存到变量中。

Not sure I exactly understand your question, but here is some code that will get the source code behind the table of interest. 不确定我是否完全理解您的问题,但是这里有一些代码会将源代码放在感兴趣的表后面。 You can extract the data of interest using functions like "instr" and "mid" 您可以使用“ instr”和“ mid”之类的函数提取感兴趣的数据

' open IE, navigate to the website of interest and loop until fully loaded
Set ie = CreateObject("InternetExplorer.Application")

With ie
    .Visible = True
    .navigate "http://support.automation.siemens.com/WW/view/en/7224052"
    .Top = 50
    .Left = 530
    .Height = 400
    .Width = 400

Do Until Not ie.Busy And ie.ReadyState = 4
    DoEvents
Loop

End With

' Assign the source code behind the page to a variable
my_var = ie.document.frames(3).document.DocumentElement.innerhtml

' Extract the url for the "Product life cycle" table
pos_1 = InStr(1, my_var, "product life cycle", vbTextCompare)
pos_2 = InStr(pos_1, my_var, "/WW/llisapi", vbTextCompare)
pos_3 = InStr(pos_2, my_var, """><", vbTextCompare)
pos_4 = InStr(pos_3, my_var, """/>", vbTextCompare)
table_url = Mid(my_var, pos_2, pos_3 - pos_2)
table_url = Replace(table_url, "amp;", "", 1, -1, vbTextCompare)
table_url = "http://support.automation.siemens.com" & table_url

' navigate to the table url
ie.navigate table_url

Do Until Not ie.Busy And ie.ReadyState = 4
    DoEvents
Loop

' assign the source code for this page to a variable and extract the desired information
my_var2 = ie.document.body.innerhtml
pos_1 = InStr(1, my_var2, "ET 200X, basic modules,", vbTextCompare)

' close ie
ie.Quit

I have had problems getting ron's code to work, I think becuase IE doesn't work easily with frames. 我在让ron的代码正常工作时遇到了问题,因为IE不适用于框架。 Below is some code that will extract some of the data from the table you have mentioned, it so far doesn't handle the diagrams. 以下是一些代码,这些代码将从您提到的表中提取一些数据,但到目前为止,它尚不处理图表。

Sub FrameStrip()


    Dim oFrames As Object
    Dim tdelements As Object
    Dim tdElement As Object
    Dim oFrame As MSHTML.HTMLFrameElement
    Dim oElement As Object
    Dim sString As String
    Dim myVar As Variant
    Dim sLinks() As String
    Dim i As Integer
    Dim bfound As Boolean
    Dim url As String
    Dim oIE As InternetExplorer


    Set oIE = New InternetExplorer

    url = "http://support.automation.siemens.com/WW/view/en/7224052"

    'Set address for use with relative source names
    myVar = Split(url, "/")
    sString = myVar(0) & "//" & myVar(2)

    oIE.navigate url
    oIE.Visible = True

    Do Until (oIE.readyState = 4 And Not oIE.Busy)
        DoEvents
    Loop

    Set oFrames = oIE.document.getElementsByTagName("frame")
    ReDim sLinks(oFrames.Length)

    'Get the source locations for each frame
    i = 0
    For Each oFrame In oFrames

       sLinks(i) = sString & (oFrame.getAttribute("src"))

       i = i + 1
    Next oFrame

    'Go through each frame to find the table
    i = 0
    bfound = False
    Do While i < UBound(sLinks) And bfound = False
        oIE.navigate sLinks(i)
        Do Until (oIE.readyState = 4 And Not oIE.Busy)
            DoEvents
        Loop

        Set oElement = oIE.document.getElementById("produktangaben")
        bfound = IsSet(oElement)
        i = i + 1
    Loop

    Set tdelements = oElement.getElementsByTagName("td")
    'Display information about table
    sString = ""
    For Each tdElement In tdelements
       Debug.Print tdElement.innerText
       sString = sString & tdElement.innerText
    Next tdElement


End Sub

Function IsSet(ByRef oElement As Object) As Boolean

    Dim tdelements As Object
    Dim bSet As Boolean

    bSet = True

    On Error GoTo ErrorSet
    Set tdelements = oElement.getElementsByTagName("td")
    On Error GoTo 0


Cleanup:

    On Error Resume Next
    Set tdelements = Nothing
    On Error GoTo 0

    IsSet = bSet

Exit Function

ErrorSet:
    bSet = False
    GoTo Cleanup:
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM