无法使用VBA从类名中删除表

时间:2016-04-29 11:14:55

标签: vba excel-vba web-scraping excel

我是VBA的新手,如果这个问题的格式很差,请不要道歉,因为我不知道如何在这里搞定。

目前,我正试图从网站上下载数据但没有成功,它出现了“运行时错误438,我确实尝试使用不同的方法

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>

有人能引导我如何解决问题吗?

非常感谢!

1 个答案:

答案 0 :(得分: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")

我希望这会有所帮助,即使它不能解释为什么你的代码无法正常工作。