2016-04-29 51 views
0

我是VBA的新手,如果此问题显示格式不正确,请致歉,因为我不知道如何在此处做得更好。无法使用VBA从班级名称中删除表格

目前,我试图从网站备案,但没有成功下载数据,它自带了一个“运行时错误438,我也尝试使用不同的metheods像

getElementsByClassName方法看为“M_box”“M_content”“pub_table”
getElementsByID寻找“datatb”

但没有成功。我也试过以下,但也未能

get.ElementByClassName( “M_content”)(0).getElementsByID( “datatb”)(1) get.ElementByClassName( “M_content”)(0)。的getElementsByTagName( “表”)(1)


Sub GetAsianOdds() 

Dim IE As Object 
Dim r As Integer, c As Integer, t As Integer, x As Integer 
Dim ElementHtml As Object 

Set IE = CreateObject("InternetExplorer.Application") 

With IE 

.Visible = True 
.navigate ("http://odds.500.com/fenxi/yazhi-567405") 

While IE.readyState <> 4 
DoEvents 
Wend 

MsgBox "IE is ready" 

Set ElementHtml = IE.Document.getElementsByClassName("pub_table") <--Run Time Error happens here 

For t = 0 To (ElementHtml.Length - 1) 
For r = 0 To (ElementHtml(t).Rows.Length - 1) 
For c = 0 To (ElementHtml(t).Rows(r).Cells.Length - 1) 
Set ThisWorkbook.Worksheets("Test").Cells(r + 1, c + 1).Value = ElementHtml(t).Rows(r).Cells(c).innerText 
Next c 
Next r 
Next t 

End With 

IE.Quit 
Set IE = Nothing 

End Sub 

并且在下面有关网站的部分的HTML代码


<div class="mar_b yz_contrast"> 
<div class="M_box"> 
<div class="M_title"><h2>...</h2></div> 
<div class="M_content"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="pub_table" id="datatb"> 
<tr> 
<th class="th_one"><label>...</label></th> 
<th>...</th> 
<th width="55">...</th> 
<th width="120">...</th> 
<th width="55">...</th> 
<th>...</th> 
<th width="55">...</th> 
<th width="90">...</th> 
<th width="55">...</th> 
<th>...</th> 
<th>...</th> 
</tr> 

任何人都可以引导我如何解决这个问题呢?

非常感谢!

回答

0

这不是一个真正的答案,但我基本上与vba html类相同,它的工作原理。所以你可能想尝试一下:

'make sure you add references to Microsoft Internet Controls (shdocvw.dll) and 
'Microsoft HTML object Library. 
'Code will NOT run otherwise. 

Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll) 
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library 
Dim htmlInput As MSHTML.HTMLInputElement 
Dim htmlColl As MSHTML.IHTMLElementCollection 




Set objIE = New SHDocVw.InternetExplorer 


With objIE 
    .Navigate "http://worldoftanks.com/en/tournaments/1000000017/" ' Main page 
    .Visible = 0 
    Do While .READYSTATE <> 4: DoEvents: Loop 
     Application.Wait (Now + TimeValue("0:00:01")) 


     Set htmlDoc = .document 

     Dim ButtonRoundData As Variant 
     Set ButtonRoundData = htmlDoc.getElementsByClassName("group-stage_link") 

我希望这可以帮助,即使它不是一个答案,为什么你的代码不能正常工作。