Gary
Gary

Reputation: 1

How to scrape table from a class name using VBA?

I am trying to download the data from the website for record. It comes out

Run-Time Error 438

I did try to use different methods
getElementsByClassName to look for "M_box","M_content" and "pub_table",
getElementsByID to look for "datatb"

I also tried the following
get.ElementByClassName("M_content")(0).getElementsByID("datatb")(1) get.ElementByClassName("M_content")(0).getElementsByTagName("Table")(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

The partial HTML code of the website.

<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>

Upvotes: 0

Views: 1242

Answers (1)

B.G.
B.G.

Reputation: 6026

this is not really an answer, but i do basically the same with the vba html classes and it works. so you maybe want to try it:

'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")

I hope this helps, even if it isn't a answer why your code doesn't work.

Upvotes: 0

Related Questions