如何从不明确的URL导入数据?

时间:2017-02-16 22:40:31

标签: vba excel-vba excel

这几乎正常工作!!但不完全!

        If link.innerHTML Like "*Upload Questionnaire*" Then
            link.Click

            Set objIE = CreateObject("InternetExplorer.Application")
            objIE.Navigate strURL

            Do While objIE.ReadyState <> 4 And objIE.Busy
               DoEvents
            Loop


                    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
                    XMLHTTP.Open "GET", strSQL, False
                    XMLHTTP.send
                    Set html = CreateObject("htmlfile")
                    html.body.innerHTML = XMLHTTP.responseText
                    Set tbl = html.getElementsByTagName("Table")
                    Set tr_coll = tbl(0).getElementsByTagName("TR")
                    For Each tr In tr_coll
                        j = 1
                        Set td_col = tr.getElementsByTagName("TD")
                        For Each td In td_col
                            Cells(row + 1, j).Value = td.innerText
                            j = j + 1
                        Next
                        row = row + 1
                    Next
        End If

首先,代码不会暂停并等待浏览器完成加载。

Do While objIE.ReadyState <> 4 And objIE.Busy
   DoEvents
Loop

另外,我正在输入父网址,如下所示:

strSQL = "https://blah_blah_blah_CampaignID=" & cell.Value

以下这行不起作用。

XMLHTTP.Open "GET", strSQL, False

不知何故,我需要传递从父URL打开的子URL。这是父网址:strSQL = "https://blah_blah_blah_CampaignID=" & cell.Value

何时:link.Click 然后运行子URL,但我不知道如何引用子URL。我怎么能这样做?!

提前致谢!

1 个答案:

答案 0 :(得分:0)

假设链接包含在类中,而不是循环遍历文档本身中的所有a元素,请使用getElementsByClassName()来获取目标类中的所有元素,然后循环遍历这些元素。

类似的东西:

For each classElement in IE.document.getElementsByClassName("thClass")
  For each linkElement in classElement.getElementsByTagName("a")
    'check for innerHTML, etc.
  Next
Next

如果课程在链接中,您只需翻转循环。

这应该有用,例如:

Public Sub LookADemo()
Dim ie As InternetExplorer, doc As HTMLDocument
Dim thisClass As IHTMLElement2, thisLink As IHTMLElement
Set ie = New InternetExplorer
ie.navigate "https://www.google.com"
Do
DoEvents
Loop Until (ie.ReadyState >= READYSTATE_INTERACTIVE) And (ie.Busy = False)
Set doc = ie.Document
For Each thisClass In doc.getElementsByClassName("hp")
  For Each thisLink In thisClass.getElementsByTagName("a")
    Debug.Print thisLink.InnerText
  Next
Next
ie.Quit
End Sub