【问题标题】:Reading webtable using selenium and python使用 selenium 和 python 读取 webtable
【发布时间】:2021-06-18 21:10:38
【问题描述】:

我需要使用 selenium 和 python 在 pandas 数据框中读取以下 webtable

部分包含列名 我已经给出了第一行的 html,如下所示
Checkbox    Sr.No.   Items    Price    

              1      5         20       
              2      3         50       

<table class="completetbclass" id="1234unique">
        <tbody>
            <th align="left>
                <a id="heaader_id1" >
                    "Sr.No."  ---header name
                    <span id="spantest1"></span>
                </a>
            </th>
            <th align="left>
                <a id="heaader_id2" >
                    Items ---header_name
                    <span id="spantest2"></span>
                </a>
            </th>
            <th align="left>
                <a id="heaader_id3" >
                    Price ----header_name
                    <span id="spantest3"></span>
                </a>
            </th>           
            <tr class="row1" id="2">
                <td align="center" class="tb">
                    <span>
                        <table class="tbclass">
                            <tbody>
                                <tr>
                                    <td class="xyz" id="tyu">
                                        <a id="yu" href="#">
                                            <span>123</span>
                                        </a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </span>
                </td>
                
                <td align="center" class="tb">
                    <span>
                        <table class="tbclass">
                            <tbody>
                                <tr>
                                    <td class="qrv",qrv_id="ab">
                                        <input value="5" type="text" id="776">
                                        
                                            
                                        
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </span>
                </td>
                
                <td align="center" class="tb">
                    <span>
                        <table class="tbclass">
                            <tbody>
                                <tr>
                                    <td class="qrv",qrv_id="24ab">
                                        <input value="20" type="text" id="7778h">
                                        
                                            
                                        
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </span>
                </td>
            </tr>
        </tbody>
</table>
'''

我需要使用 selenium 和 python 阅读上面的 webtable。我已经放了第一行的 html。 目前我正在尝试使用 pandas 数据框读取它,但我不能。按照代码

webtable=driver.find_element_by_xpath('//*table[@id="1234unique"]').get_attribute("outerHTML")
df=pd.read_html(webtable}

但是这段代码不起作用。在 pandas 的表格中读取这个 webtable 的替代方法是什么?


【问题讨论】:

    标签: python pandas selenium


    【解决方案1】:

    如果没有指向您的数据的实际链接(也许这是一个 Intranet 站点),我无法测试这些代码示例,但您当然可以。

    import requests
    import pandas as pd
    
    url = 'https://www.federalreserve.gov/releases/h8/current/default.htm'
    html = requests.get(url).content
    df_list = pd.read_html(html)
    df = df_list[10]
    print(df)
    
    
    ############################################################################
    
    
    import requests
    from bs4 import BeautifulSoup
     
    url = "https://en.wikipedia.org/wiki/List_of_United_States_cities_by_population"
    r = requests.get(url)
    html = r.text
    soup = BeautifulSoup(html, "html.parser")
    table = soup.find_all('table')
    print(table)
    
    
    ############################################################################
    
    
    from bs4 import BeautifulSoup
    import urllib.request
    
    url = "http://biz.yahoo.com/c/s.html"
    page = urllib.request.urlopen(url)
    soup = BeautifulSoup(page.read(), "lxml")
    
    alltables = soup.find_all("table")
    print(alltables)
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2017-05-04
      • 2021-11-12
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多