2017-01-03 38 views
0

我是新来的,需要一些建议为我的模块。 我已经创建了下面的模块,用于从与alexa.com特定地址刮数据值: alexa.com/siteinfo/clashofclans.comVBA - 从HTMl表中获取​​innertextxt如果条件第一个td在行为真

具体值嵌套在下面的表中: http://imgur.com/JB11PT2

我试图从美国获得数据“访客百分比”,其值为9.1%,但代码仅在美国位于第一行/第一位时才有效。 http://imgur.com/yMBmdbs

下面的VBA代码是我尝试在刮:

Sub ExtractAlexa() 
    Dim tickername As String 
    Dim doc As HTMLDocument 

    ie.Visible = False 
    ie.navigate "http://www.alexa.com/siteinfo/clashofclans.com" 

    Do 
    DoEvents 
    Loop Until ie.readyState = READYSTATE_COMPLETE 

    Application.Wait (Now + TimeValue("00:00:4")) 
    Set doc = ie.document 

    Set elems = doc.getElementById("demographics_div_country_table").getElementsByTagName("tr") 
    For Each e In elems   
    If e.outerHTML Like "*/topsites/countries/US*" Then 
     Sheet2.Range("E11").Value = Trim(doc.getElementsByTagName("td")(1).innerText) 
    End If 
    Next e 

    ie.Quit 
End Sub 

请,没有人知道我要去的地方错在这里? 谢谢。

回答

0

你已经基本上钉 - 但我认为你在这行个微妙的问题:

Sheet2.Range("E11").Value = Trim(doc.getElementsByTagName("td")(1).innerText) 

应该是:

Sheet2.Range("E11").Value = Trim(e.getElementsByTagName("td")(1).innerText) 

您也可以使用WinHTTP避免一些与IE扯皮:

Public Sub ExtractAlexa() 
    Dim oHTML As MSHTML.HTMLDocument 
    Dim elems As MSHTML.IHTMLElementCollection 
    Dim e As MSHTML.IHTMLElement 
    Set oHTML = New MSHTML.HTMLDocument 
    With CreateObject("WINHTTP.WinHTTPRequest.5.1") 
     .Open "GET", "http://www.alexa.com/siteinfo/clashofclans.com", False 
     .send 
     oHTML.body.innerHTML = .responseText 
    End With 
    oHTML.getElementById("demographics_div_country_table").getElementsByTagName ("tr") 
    Set elems = oHTML.getElementById("demographics_div_country_table").getElementsByTagName("tr") 
    For Each e In elems 
     If e.outerHTML Like "*/topsites/countries/US*" Then 
      Sheet2.Range("E11").Value = Trim(e.getElementsByTagName("td")(1).innerText) 
      Exit For 
     End If 
    Next e 
End Sub 
+0

谢谢你,为了你的建议,我的模块现在正在工作。对此,我真的非常感激。 – eros

相关问题