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")))
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
我在stackoverflow上搜索这个问题,看到了这个帖子
BeautifulSoup returning none when element definitely exists
通过阅读luiyezheng提供的答案,我得到了一个提示,可能是在获取数据时动态的。所以,表可能是动态创建的,因此我找不到。
所以,解决办法是:
在存储网页之前,我推迟了一段时间
所以代码是这样的
我希望它能帮助别人。
所需的表可能是动态生成的,因此您需要等到它出现在页面上:
另外请注意,没有必要使用BeautifulSoup,因为Selenium有足够的内置方法和属性来为您完成相同的工作,例如
^{pr2}$根据您共享的刮取} 来归纳{a1},为了实现这一点,您可以使用以下代码块之一:
<table>
的HTML,您将expected_conditions
子句设置为^{使用
class
:使用
id
:相关问题 更多 >
编程相关推荐