【问题标题】:Extracting html table into panda DataFrame using Selenium使用 Selenium 将 html 表提取到 panda DataFrame
【发布时间】:2021-09-07 20:45:03
【问题描述】:

我正在尝试读取一个 html 表,然后将其提取到 pd.DataFrame 中,但却得到了一些不同的东西。我做错了什么?

错误是:[]

这是我的代码:

import pandas as pd
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu') 
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe', options = options) 
driver.get("https://ai.fmcsa.dot.gov/SMS")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Close']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "(//input[@name='MCSearch'])[2]"))).send_keys('1818437')
wait.until(EC.element_to_be_clickable((By.XPATH, "(//input[@name='search'])[2]"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='BASICs']/p[2]/a"))).click()
tables=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,'//*[@id="BASICs"]/table/tbody/tr[2]')))
print(tables)

忽略一堆额外的导入,因为我一直在尝试以不同的方式解决问题,但一直失败

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    其实我可能已经解决了。

    我改变了表格的 XPATH 来自

    '//*[@id="BASICs"]/table/tbody/tr[2]'
    

    "//tr[@class='valueRow sumData']"
    

    我刚刚意识到我正在打印元素而不是它的内容,所以我将最后一行从 print(tables)print(tables.text)

    现在它正在打印表格,但由于某种原因它没有打印最后一个值(在本例中为 0.05)。任何想法为什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多