如何解决使用硒进行网页搜集数据
您好,我正在抓取此页面https://www.betexplorer.com/soccer/china/super-league-2016/beijing-guoan-henan-jianye/S49KzkvO/,我必须抓取这些数据
Country = driver.find_element_by_xpath("/html/body/div[4]/div[4]/div/div/div[1]/section/ul[1]/li[3]/a").text
leagueseason = driver.find_element_by_xpath("/html/body/div[4]/div[4]/div/div/div[1]/section/header/h1/a").text
Home = driver.find_element_by_xpath("/html/body/div[4]/div[4]/div/div/div[1]/section/ul[2]/li[1]/h2/a").text
Away = driver.find_element_by_xpath("/html/body/div[4]/div[4]/div/div/div[1]/section/ul[2]/li[3]/h2/a").text
我尝试使用这些XPATH,但是我会使用更具体的XPath,因为这可能会改变。有什么建议吗?谢谢
解决方法
要打印document.addEventListener("DOMContentLoaded",() => {
document.querySelector(".icon-button").forEach((button) => {
button.addEventListener('click',()=>{
...
});
});
});
的元素,您必须为innerText
引入WebDriverWait,并且可以使用以下Locator Strategies中的任何一个:
-
使用css-selectors和
visibility_of_element_located()
:-
中国:
get_attribute("innerHTML")
-
2016年超级联赛:
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"ul.list-breadcrumb li:nth-child(3) a"))).get_attribute("innerHTML"))
-
北京国安:
print(WebDriverWait(driver,"h1.wrap-section__header__title>a"))).get_attribute("innerHTML"))
-
河南建业:
print(WebDriverWait(driver,"ul.list-details>li:first-child h2.list-details__item__title>a"))).get_attribute("innerHTML"))
-
-
使用xpath和 text 属性:
-
中国:
print(WebDriverWait(driver,"ul.list-details>li:nth-child(3) h2.list-details__item__title>a"))).get_attribute("innerHTML"))
-
2016年超级联赛:
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//ul[@class='list-breadcrumb']//following::li[3]//a"))).text)
-
北京国安:
print(WebDriverWait(driver,"//h1[@class='wrap-section__header__title']/a"))).text)
-
河南建业:
print(WebDriverWait(driver,"//ul[@class='list-details']//following::li[1]//h2/a"))).text)
-
注意:您必须添加以下导入:
print(WebDriverWait(driver,"//ul[@class='list-details']//following::li[2]//h2/a"))).text)
-
您可以在How to retrieve the text of a WebElement using Selenium - Python
中找到相关的讨论
Outro
链接到有用的文档:
-
get_attribute()
方法from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
-
text
属性返回Gets the given attribute or property of the element.
- Difference between text and innerHTML using Selenium
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。