本文介绍了VBA HTML抓取-复杂表中的".innertext"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我创建了以下模块,以从以下地址中提取单个值(伦敦房屋价格变化1m%):

I've created the following Module to scrape a single value (1m % change in London house prices) from the below address:

https://www.hometrack.com/uk/insight/uk-cities-house-price-index/

特定值嵌套在以下代码中:

The specific value is nested within the following code:

下面的VBA代码是我尝试进行抓取的尝试.我(也许是错误地)觉得我非常接近获取价值-但是代码无法正常工作.

The below VBA code is my attempt at scraping. I, perhaps wrongly, feel that I am very close to capturing the value - but the code will not work.

有人知道我在哪里错吗?它不会显示错误消息,但也不会输出任何值.

Does anyone know where I am going wrong here? It doesn't show an error message but also doesn't output any values.

 Sub HousePriceData()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim TxtRng As Range
        Dim ie As Object
        Dim V As Variant
        Dim myValue As Variant

        Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
        ie.NAVIGATE "https://www.hometrack.com/uk/insight/uk-cities-house-price-index/"
        ie.Visible = False

        While ie.ReadyState <> 4
            DoEvents
        Wend

        Set wb = ActiveWorkbook
        Set ws = wb.Sheets("Input")
        Set TxtRng = ws.Range("C15")

        Set myValue = ie.document.getElementById("cities-index-table").getElementsByTagName("tr")(7).g‌​etElementsByTagName("td")(5)

        TxtRng = myValue.innerText
        End Sub

推荐答案

尝试使用XHR和原始解析而不是笨拙的IE:

Try to use XHR and primitive parsing instead of awkward IE:

Sub Test()

    Dim strUrl As String
    Dim strTmp As String
    Dim arrTmp As Variant

    strUrl = "https://www.hometrack.com/uk/insight/uk-cities-house-price-index/"
    With CreateObject("MSXML2.XMLHttp")
        .Open "GET", strUrl, False
        .Send ""
        strTmp = .ResponseText
    End With
    arrTmp = Split(strTmp, ">London</a></td>", 2)
    strTmp = arrTmp(1)
    arrTmp = Split(strTmp, "<td>", 7)
    strTmp = arrTmp(6)
    arrTmp = Split(strTmp, "</td>", 2)
    strTmp = arrTmp(0)

    ThisWorkbook.Sheets("Input").Range("C15").Value = strTmp

End Sub

这篇关于VBA HTML抓取-复杂表中的".innertext"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:05
查看更多