To extract information from a table?

0

I need to extract the information from a table with selenium, python.

Cuepo html:

<table class="table table-bordered">
                    <tbody>
                        <tr>
                            <td>Valor de la Prima</td>
                            <td style="text-align: right;">$ 1.176.256</td>
                        </tr>
                        <tr>
                            <td>IVA</td>
                            <td style="text-align: right;">$ 223.489</td>

                        </tr>
                        <tr>
                            <td><strong>Total Prima</strong></td>
                            <td style="text-align: right;"><strong>$ 1.399.745</strong></td>
                        </tr>

                    </tbody>
                </table>

There are two tables with the same class, the first and this one to which I want to extract information.

I had a code but with beautiful and they recommended me better to do it with selenium directly.

Thanks

    
asked by Diego Lopez 29.12.2017 в 00:21
source

1 answer

1

With the following code you can print the information of the table:

# obtenemos los elementos que satisfacen la clase enviada como argumento
table = driver.find_elements_by_class_name('table table-bordered')
# obtener todas las filas de la tabla
rows = table[1].find_elements_by_tag_name('tr') # indexamos en 1 ya que dices que necesitas la segunda tabla
# iteramos en cada una de las filas almacendas en rows
for row in rows:
    col = row.find_elements_by_tag_name('td')[1] # se puede indexar con 0, 1, 2... depende del tamanio de la tabla y la columna que queramos
    print(col.text) # imprimimos el texto de los elementos
    
answered by 29.12.2017 / 00:50
source