在html表行VBA中获取第三个元素

问题描述 投票:-2回答:1

我正在开展一个个人项目,我从网站上获取一些信息。该网站有一张桌子。我知道如何循环遍历每一行,对于每一行,我想将信息与第一列和第四列的内容进行比较,并且两者都与我表中的信息匹配,将该行中第五列的内容复制到我的行中表但不知道如何去做这件事。这就是表格html中每一行的样子。

<tr class="player_tr_1" data-url="/18/player/1/Pelé">
    <td class="table-row-text" style="text-align: left;">
      <img style="padding: 0;" class="player_img player_right_curve 
        form rating icon gold rare" src="./FIFA 18 Players _ 
        FUTBIN_files/237067.png">                                    
 //Check info here 
       <a href="https://www.futbin.com/18/player/1/Edson%20Arantes" 
         class="player_name_players_table"> CHECK THIS TEXT </a>
     </td> <td><span class="form rating icon gold rare">98</span> </td>
     <td class="">CAM</td>
  //Check info here
     <td class="">CHECK THIS INFO</td>
  //Grab info here
     <td><span class="ps4_color"> GRAB THIS INFO </span></td>
     <td><span class="xb1_color">0</span></td>
     <td><span class="pc_color">0</span></td>
     <td><span class="yellow_players_stat">76</span></td>
     <td>173cm | 5'8"</td>
     <td>77</td>
     <td>516</td>
     <td>2513</td>
</tr>

这是我到目前为止的代码:它遍历表中的每一行,在该循环中我需要从该行中获取信息。我在循环中评论了我需要做什么

Private Sub CommandButton1_Click()
        SearchBot
End Sub

'start a new subroutine called SearchBot '
Sub SearchBot()

'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLHtmlElement
Dim X As Integer 'integer variable we'll use as a counter
Dim version As String 'string variable that will hold our version '
Dim NumRows As Integer


'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True



' Set numrows = number of rows of data.
NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count + 1
' Select cell a1.
Range("A2").Select
' Establish "For" loop to loop "numrows" number of times.

' screen is updated every time a new price is inserted '
Application.ScreenUpdating = True

For X = 2 To NumRows
    ' go to player page with correct name '
    objIE.navigate "https://www.futbin.com/18/players?page=1&search=" & Sheets("Sheet1").Range("A" & X).Value


    ' wait '
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


    ' loop through each row '
    For Each aEle In objIE.document.getElementsByTagName("tr")
        ' check player name, if correct loop through table to grab price if it is also the right version maybe or'
        ' or grab first elements text content, check if correct name, check 4th element for correct version, grab 5th element, and insert in table '

        'if 1st element = Sheets("Sheet1").Range("A" & X).Value & "" '
        ' And 4th element = Sheets("Sheet1").Range("B" & X).Value Then '
        'Sheets("Sheet1").Range("F" & X).Value = 5th element '
    Next
Next
' click the correct version of the player '


'close the browser
objIE.Quit
MsgBox ("Done")

'exit our SearchBot subroutine '
End Sub

编辑:正如你在html中看到的那样,我试图抓取的元素没有类名。

提前致谢!

excel vba excel-vba
1个回答
0
投票

似乎只有一个没有任何ID /类的元素可用于识别它,因此您必须通过索引获取,而其余的按类名获取。

Dim FirstColumn As String
Set FirstColumn = aEle.getElementsByClassName("player_name_players_table")(1).innerText

Dim FourthColumn As String
Set FourthColumn = aEle.getElementsByTagName("td")(4).innerText

Dim FifthColumn As String
Set FifthColumn = aEle.getElementsByClassName("ps4_color")(1).innerText

请注意,我不习惯VBA(我是VB.NET的人)所以我不确定所有语法是否正确,也不确定返回的数组是从零还是一开始。 :)

© www.soinside.com 2019 - 2024. All rights reserved.