find()
方法来提取表格。例如,假设一个表格包含在一个 则需要使用以下代码来提取表格: 以上是两种常见的解决方法,但实质上无法提取表格的原因可能有很多种。如果以上方法不奏效,还需要根据具体情况进行针对性调试。
...
div = soup.find('div')
table = div.find('table')
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# 启动浏览器
driver = webdriver.Chrome()
# 访问网页
driver.get("https://example.com")
# 等待表格加载完成
table_element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "table")))
# 将页面源代码传递给BeautifulSoup进行解析
soup = BeautifulSoup(driver.page_source, 'html.parser')
# 提取表格
table = soup.find('table')
# 关闭浏览器
driver.quit()
相关内容