从网站提取数据作为单独的字段

时间:2017-01-09 12:55:31

标签: excel vba excel-vba

我正在尝试使用excel从网站上删除一些信息。

这是在源上显示的内容:

Replace

所以我希望获得所有具有偶数的内容,并在<tr class="even"> <td align="right">1</td> <td><a href="/kld/en/1.html">Acrobatic Maneuver</a></td> <td>Instant</td> <td>2W</td> <td>Common</td> <td>Winona Nelson</td> <td><img src="http://magiccards.info/images/en.gif" alt="English" width="16" height="11" class="flag2"> Kaladesh</td> </tr>

之间提取数据

然而,直到现在我发现的只是这段代码

<td></td>

它的工作方式是提取数据,但输出总是Sub getcards() Dim IE As Object Dim i As Long Dim objCollection As Object ' Create InternetExplorer Object Set IE = CreateObject("InternetExplorer.Application") ' You can uncoment Next line To see form results IE.Visible = False ' URL to get data from IE.Navigate "http://magiccards.info/query?q=%2B%2Be%3Akld/en&v=list&s=issue" ' Statusbar Application.StatusBar = "Loading, Please wait..." ' Wait while IE loading... Do While IE.Busy DoEvents Application.Wait DateAdd("s", 1, Now) Loop On Error GoTo abort Application.StatusBar = "Searching for value. Please wait..." Dim dd As String Set objCollection = IE.document.getElementsByClassName("even") For i = 0 To objCollection.Length dd = IE.document.getElementsByClassName("even")(i).innerText MsgBox dd Next i abort: ' Show IE IE.Visible = True IE.Quit ' Clean up Set IE = Nothing Application.StatusBar = "" End Sub

我怎么能这样做,它将每个1Acrobatic ManeuverInstant2WCommonWinona Nelson Kaladesh理解为一个单独的字段,所以我可以轻松地提取它?

1 个答案:

答案 0 :(得分:4)

当您在i内循环objCollection时,实际上是使用ClassName“偶数”循环遍历所有元素,而不是您想要的特定元素中的元素

试试这个:

For i = 0 To objCollection.Length - 1
    For c = 0 to IE.document.getElementsByClassName("even")(i).getElementsByTagName("td").Length - 1
        dd = IE.document.getElementsByClassName("even")(i).getElementsByTagName("td")(c).innerText
        MsgBox dd
    Next c
Next i