from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
table=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'table'))) #表格元素
rows = table.find_elements(By.CSS_SELECTOR, "tbody tr") #表数据行
thead_rows = table.find_elements(By.CSS_SELECTOR, "thead tr th") #表头
data=[]
for i in range(len(rows)):
columns = rows[i].find_elements(By.CSS_SELECTOR, 'td')
row_data=[]
for col in range(len(columns)):
row_data.append(columns[col].text)
data.append(row_data)
df = pd.DataFrame(data, columns=[thead_rows[col].text for col in range(len(thead_rows))])
df.to_excel('output.xlsx', index=False)
获取html页面表格数据并存到Excel中
最新推荐文章于 2024-07-28 23:09:07 发布