【问题标题】:Reading tables with Pandas and SeleniumPandas 和 Selenium 的阅读表
【发布时间】:2021-03-04 11:08:23
【问题描述】:

我正在尝试找出一种从网站上抓取表格的优雅方法。但是,当在脚本下运行时,我收到 ValueError: No tables found 错误。

from selenium import webdriver
import pandas as pd

driver = webdriver.Chrome(executable_path=r'C:...\chromedriver.exe')
driver.implicitly_wait(30)

driver.get("https://www.gallop.co.za/#meeting#20201125#3")

df_list=pd.read_html(driver.find_element_by_id("eventTab_4").get_attribute('outerHTML'))

当我查看站点元素时,我注意到如果

标记整齐地位于
中,则下面的代码可以正常工作。但是,在这种情况下,我认为代码无法正常工作,原因如下:
  1. 中有一个
    ,然后是
标签。
  • 该网站对表格使用 Javascript。
  • 感谢您提供有关如何为所有比赛拉表的建议。也就是说,当用户单击每个选项卡(比赛)时,有几个表格可见。我需要将它们全部提取到单独的数据框中。

    【问题讨论】:

    • 问题是如果你点击eventTab_4中的href ..它会打开一个没有表格的新网页。如果您手动单击它,则会显示一个带有表格的新页面。让我看看我能不能解决。

    标签: python pandas selenium


    【解决方案1】:
    from selenium import webdriver
    import time
    import pandas as pd
    
    pd.set_option('display.max_column',None)
    driver = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
    driver.get("https://www.gallop.co.za/#meeting#20201125#3")
    time.sleep(5)
    tab = driver.find_element_by_id('tabs') #All tabs are here
    li_list = tab.find_elements_by_tag_name('li')  #They are in a "li"
    a_list = []
    for li in li_list[1:]:  #First tab has nothing..We skip it
        a = li.find_element_by_tag_name('a')  #extract the "a" element from the "li"
        a_list.append(a)
    
    df = []
    for a in a_list:
        a.click()    #Next Tab
        time.sleep(8)  #Tables take some time to load fully
        page = driver.page_source   #Get the HTML of the new Tab page
        source = pd.read_html(page)
        table = source[1]    #Get 2nd table
        df.append(table)
    
    print(df)
    

    输出

     [   Silk  No              Horse  Unnamed: 3   ACS  SH  CFR         Trainer  \
    0   NaN   1           JEM ROCK         NaN   4bC   A  NaN      Eric Sands   
    1   NaN   2       MAISON MERCI         NaN  3chC   A  NaN  Brett Crawford   
    2   NaN   3         WORDSWORTH         NaN   3bC  AB  NaN     Greg Ennion   
    3   NaN   4    FOUND THE DREAM         NaN   3bG   A  NaN     Adam Marcus   
    4   NaN   5             IZAPHA         NaN   3bG   A  NaN       Andre Nel   
    5   NaN   6        JACKBEQUICK         NaN  3grG   A  NaN     Glen Kotzen   
    6   NaN   7           MHLABENI         NaN  3chG   A  NaN      Eric Sands   
    7   NaN   8              ORLOV         NaN   3bG   A  NaN     Adam Marcus   
    8   NaN   9           T'CHALLA         NaN   3bC   A  NaN   Justin Snaith   
    9   NaN  10  WEST COAST WONDER         NaN   3bG   A  NaN      Piet Steyn   
    
                     Jockey  Wgt  MR  Dr  Odds       Last 3 Runs  
    0              D Dillon   60   0   7   NaN               NaN  
    

    continued

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签