0

I am using excel with VBA to open a page and extract some information and putting it in my database. After some research, I figured out that opening IE obviously takes more time and it can be achieved using XmlHTTP. I am using the XmlHTTP to open a web page as proposed in my another question. However, while using IE I was able to navigate through div tags. How can I accomplish the same in XmlHTTP?

If I use IE to open the page, I am doing something like below to navigate through multiple div elements.

Set openedpage1 = iedoc1.getElementById("profile-experience").getElementsbyClassName("title")
For Each div In openedpage1
---------

However, with XmlHttp, I am not able to do like below.

For Each div In html.getElementById("profile-experience").getElementsbyClassName("title")

I am getting an error as object doesn't support this property or method.

4

3 回答 3

0

看看我为另一个问题发布的这个答案,因为这与您正在寻找的内容很接近。总之,您将:

  1. 创建一个Microsoft.xmlHTTP对象

  2. 使用 xmlHTTP 对象打开您的网址

  3. 将响应作为 XML 加载到DOMDOcument对象中

从那里您可以XMLNodesDOMDocument

于 2013-07-18T21:32:35.997 回答
0

XMLHttp 对象将页面内容作为responseText. 您将需要解析此字符串以找到您需要的信息。正则表达式是一种选择,但它会很麻烦。

此页面使用字符串函数(Mid、InStr)从 html 文本中提取信息。

可能可以从检索到的 HTML 中创建一个 DOMDocument(我相信是这样),但我没有追求这一点。

于 2013-07-18T21:32:49.393 回答
0

如上面的答案所述,将其.responseText放入 HTMLDocument 中,然后使用该对象,例如

Option Explicit
Public Sub test()
    Dim html As HTMLDocument
    Set html = New HTMLDocument

    With CreateObject("WINHTTP.WinHTTPRequest.5.1")
        .Open "GET", "http://www.someurl.com", False
        .send
        html.body.innerHTML = .responseText
    End With

    Dim aNodeList As Object, iItem As Long
    Set aNodeList = html.querySelectorAll("#profile-experience.title")
    With ActiveSheet
        For iItem = 0 To aNodeList.Length - 1
            .Cells(iItem + 1, 1) = aNodeList.item(iItem).innerText
            '.Cells(iItem + 1, 1) = aNodeList(iItem).innerText '<== or potentially this syntax
        Next iItem
    End With
End Sub

笔记:

我已经将你的字面意思翻译getElementById("profile-experience").getElementsbyClassName("title")CSS 选择器querySelectorAll("#profile-experience.title")所以假设你已经正确地做到了。

于 2018-06-26T08:44:27.910 回答