提问者:小点点

找不到使用selenium的表


你好,我想从一个网站报废数据。我使用

 BeautifulSoup 

这是我使用的代码(没有导入):

df = pd.read_html(requests.get('myurl').text, flavor="bs4")
df = pd.concat(df)
df.to_csv("mycsv.csv", index=False)

到目前为止我没有这个代码的问题,但当我想从这个站点报废数据。上面的程序有一个错误,说找不到表。所以我用

selenium   

来解决我的问题。下面是代码:

driver = webdriver.Firefox(executable_path=r'C:\Users\myfolders\geckodriver.exe')

driver.get("https://www.nba.com/stats/teams/traditional/?sort=W_PCT&dir=-1")
html = driver.page_source
tables = pd.read_html(html)
data = tables[1]
driver.close()

但是,当我执行adove代码时,我又遇到了同样的问题

ValueError: No tables found

当我检查页面的html时,我会找到表属性。有谁能帮我解决这个问题吗?


共1个答案

匿名用户

在读取driver.page_source之前,可能需要等待加载表。在我的机器上测试了以下内容,并且能够拿起两张桌子。您可能希望根据需要添加额外的等待。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

import pandas as pd

driver = webdriver.Chrome()

driver.get("https://www.nba.com/stats/teams/traditional/?sort=W_PCT&dir=-1")

table = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.TAG_NAME, 'table'))
    )

html = driver.page_source
tables = pd.read_html(html)
driver.close()
print(tables)