使用Excel VBA从网站(HTML div类)中提取值

时间:2017-01-09 23:28:17

标签: html excel vba

我尝试自动手动访问此网站并每个月从多个应用中提取评级以进行跟踪。

我已经弄明白了如何导航并登录我想要的页面,但是我在拉取实际元素方面遇到了麻烦 - 数字" 3.3"在这种情况下 - 从这个特定的部分变成excel。

由于不熟悉VBA中的HTML,我只是接受了以下教程/其他问题,但还未能找到有用的东西。

Rating on website and the code behind it     

Sub PullRating()

Dim HTMLDoc As HTMLDocument Dim ie As InternetExplorer Dim oHTML_Element As IHTMLElement Dim sURL As String On Error GoTo Err_Clear sURL = "https://www.appannie.com/account/login/xxxxxxxxxx" Set ie = New InternetExplorer ie.Silent = True ie.navigate sURL ie.Visible = True Do 'Wait until the Browser is loaded Loop Until ie.readyState = READYSTATE_COMPLETE Set HTMLDoc = ie.Document HTMLDoc.all.Email.Value = "xxxxxxxxx@xxx.com" HTMLDoc.all.Password.Value = "xxxxx" For Each oHTML_Element In HTMLDoc.getElementById("login-form") If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For Next Dim rating As Variant Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding") Range("A1").Value = rating 'ie.Refresh 'Refresh if required Err_Clear: If Err <> 0 Then Err.Clear Resume Next End If End Sub

2 个答案:

答案 0 :(得分:0)

下面的代码将允许您从第一个元素中提取文本,其中包含类名&#34; rating-number ng-binding&#34;在HTML文档中。顺便说一句,从IE 9.0开始支持GetElementsByClassName。在我的示例中,我也使用与旧版本兼容的编码。

Dim htmlEle1 as IHTMLElement

For Each htmlEle1 in HTMLDoc.getElementsByTagName("div")
   If htmlEle1.className = "rating-number ng-binding" then
      Range("A1").Value = htmlEle1.InnerText
      Exit For
   End if
Next htmlEle1

答案 1 :(得分:0)

如果您想使用已编写的代码,Ryszards代码应该可以解决问题,那么我认为您需要进行更改。

For Each oHTML_Element In HTMLDoc.getElementById("login-form")
    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

'Need to wait for page to load before collecting the value
Loop Until ie.readyState = READYSTATE_COMPLETE

Dim rating As IHTMLElement
Set rating = HTMLDoc.getElementsByClassName("rating-number ng-binding")
'Need to get the innerhtml of the element
Range("A1").Value = rating.innerhtml