使用Selenium na python从HTML提取第二张表

时间:2018-06-22 08:01:41

标签: python selenium selenium-webdriver

我正在使用Selenium和python从HTML提取表,但是我只能提取第一个表。我要提取第二张表,我也是新的T硒。

HTML代码

<div class="fondo-top">
        <h3>Flood Forecasted Site</h3>
        <div class="editorwys">



        <h4>Site Name : Araria</h4> 
                <table style="width: 100%">
                    <tbody><tr>
                        <th style="width: 25%"><strong>District Name:</strong></th>
                        <td style="width: 25%"><strong>Araria</strong></td>

                            <th style="width: 25%"><strong>Warning Level (WL):</strong></th>
                            <td style="width: 25%"><strong> Meters (m)</strong></td>

                    </tr>
                    <tr>
                        <th><strong>River Name:</strong></th>
                        <td><strong>Mahananda</strong></td>

                            <th><strong>Danger Level (DL):</strong></th>
                            <td><strong> Meters (m)</strong></td>

                    </tr>
                    <tr>
                        <th><strong>Basin Name:</strong></th>
                        <td><strong>GANGA<strong></strong></strong></td>

                        <th><strong>Highest Flood Level (HFL):</strong></th>
                        <td><strong>49.4 Meters (m)<strong></strong></strong></td>

                    </tr>
                    <tr>
                        <th><strong>Division Name:</strong></th>

                        <td><a target="_blank" href="/opencms/opencms/ffs/Detail?code=029-mgd4ptn " onclick="window.open(this.href, this.target, 'width=1000, height=600, toolbar=no, resizable=no'); return false;">Lower Ganga Division-I(LGD-I), Patna</a></td>

                        <th><strong>HFL Attained date:</strong></th>
                        <td>14-08-2017</td>

                    </tr>
                </tbody></table>
                <p>&nbsp;</p>
                <table>
                    <tbody><tr>
                        <th colspan="3" style="text-align: center;"><strong>PRESENT WATER LEVEL</strong></th>
                    </tr>

                    <tr>                            

                        <td class="" style="width:33%; height:18px;">Date: 22-06-2018 08:00</td>
                        <td class="" style="width:33%;">Value: 45.34 Meters (m)</td>
                        <td class="" style="width:33%;">Trend: Steady</td>
                    </tr>
                    <tr>
                        <th colspan="3" style="text-align: center;"><strong>CUMULATIVE DAILY RAINFALL</strong></th>
                    </tr>
                    <tr>

                            <td style="width:33%; height:18px;">Date: 22-06-2018 08:30</td>
                            <td style="width:33%;">Value: 5.0 Milimiters (mm)</td>
                            <td style="width:33%;"></td>

                    </tr>
                </tbody></table>                            
                    <p>&nbsp;</p>                       



                                <table style="width: 100%">
                                    <tbody><tr>
                                        <th colspan="4" style="text-align: center;"><strong>NO FLOOD FORECAST</strong></th>
                                    </tr>
                                </tbody></table>



            <div class="botonera">

                <a href="/ffs/data-flow-list-based/" class="mas-info"> Go Back</a>

            </div>
    </div>
</div>

我正在使用driver.find_element_by_css_selector('table > tbody').text提取第一个表。现在如何提取第二张表和第三张表???我真的会在这方面提供任何帮助

1 个答案:

答案 0 :(得分:1)

您可以使用find_elements_by_css_selector查找所有已建立表的多个元素(这些方法将返回一个列表),而不是对一个元素find_element_by_css_selector进行操作,请尝试

for form_body in driver.find_elements_by_css_selector('table > tbody'):
    print(form_body.text)