无法使用BeautifulSoup获取数据

2024-06-08 06:02:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Selenium登录网页 我能拿到那页。 我在html中搜索了一个我想刮取的表。 在这里:

<table cellspacing="0" class=" tablehasmenu table hoverable sensors" id="table_devicesensortable">

以下是脚本:

^{pr2}$

我可以在souppage变量中得到解析过的网页。 但不能刮取并存储在tbody变量中。在


Tags: 脚本id网页htmlseleniumtableclasssensors
3条回答

我在stackoverflow上搜索这个问题,看到了这个帖子

BeautifulSoup returning none when element definitely exists

通过阅读luiyezheng提供的答案,我得到了一个提示,可能是在获取数据时动态的。所以,表可能是动态创建的,因此我找不到。

所以,解决办法是:

在存储网页之前,我推迟了一段时间

所以代码是这样的

time.sleep(4)
rawpage=driver.page_source #storing the webpage in variable
souppage=BeautifulSoup(rawpage,"html.parser") #parsing the webpage
tbody=souppage.find("table",{"id":"table_devicesensortable"}) #scrapping

我希望它能帮助别人。

所需的表可能是动态生成的,因此您需要等到它出现在页面上:

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

tbody = wait(driver, 10).until(EC.presence_of_element_located((By.ID, "table_devicesensortable")))

另外请注意,没有必要使用BeautifulSoup,因为Selenium有足够的内置方法和属性来为您完成相同的工作,例如

^{pr2}$

根据您共享的刮取<table>HTML,您将expected_conditions子句设置为^{}来归纳{a1},为了实现这一点,您可以使用以下代码块之一:

  • 使用class

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//table[@class=' tablehasmenu table hoverable sensors' and @id='table_devicesensortable']")))
    rawpage=driver.page_source #storing the webpage in variable
    souppage=BeautifulSoup(rawpage,"html.parser") #parsing the webpage
    tbody=souppage.find("table",{"class":" tablehasmenu table hoverable sensors"}) #scrapping
    
  • 使用id

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//table[@class=' tablehasmenu table hoverable sensors' and @id='table_devicesensortable']")))
    rawpage=driver.page_source #storing the webpage in variable
    souppage=BeautifulSoup(rawpage,"html.parser") #parsing the webpage
    tbody=souppage.find("table",{"id":"table_devicesensortable"}) #scrapping
    

相关问题 更多 >

    热门问题