网络爬行Accuweather

时间:2018-05-02 11:47:33

标签: vba web-scraping web-crawler

我想从网站

捕捉当前天气(图片中的88)信息

Check the image

https://www.accuweather.com/en/in/bengaluru/204108/weather-forecast/204108

我使用了以下代码

Sub Get_Price()
Dim HTTP As New XMLHTTP60, HTML As New HTMLDocument
Dim post As HTMLDivElement

With HTTP
.Open "GET", "https://www.accuweather.com/en/in/india-weather", False
.send
HTML.body.innerHTML = .responseText
MsgBox .responseText
End With

For Each post In HTML.getElementsByClassName("panel-list cityforecast")
With post.getElementsByTagName("large-temp")
 If .Length Then R = R + 1: Cells(R, 1) = .Item(0).innerText
End With
Next post
End Sub

请帮助,提前致谢

1 个答案:

答案 0 :(得分:1)

尝试以下方法获取您要从该页面解析的信息。我在脚本中使用.querySelectorAll()使其简洁但更有效。试一试。

Sub GetWeatherInfo()
    Dim HTTP As New XMLHTTP60, HTML As New HTMLDocument
    Dim post As Object

    With HTTP
        .Open "GET", "https://www.accuweather.com/en/in/bengaluru/204108/weather-forecast/204108", False
        .send
        HTML.body.innerHTML = .responseText
    End With

    Set post = HTML.querySelectorAll("#feed-tabs .large-temp")(0)
    MsgBox post.innerText
End Sub

参考添加到库:

Microsoft XML, V6.0 ''or the version you have
Microsoft HTML Object Library

顺便说一句,在运行脚本之前,请确保我使用的URL是正确的。