Python-Selenium-无法从HTML网页抓取特定文本内容

扬克兹

我尝试webscrape的这部分html:

<td class="zebraTable__td zebraTable__td--companyName"><a href="/unternehmen/8116602/schneider-electric-holding-germany-gmbh" data-gtm="companySearch__searchResult--76">
                        Schneider Electric Holding Germany GmbH
                    </a></td>

HTML代码

从此站点:

https://de.statista.com/companydb/suche?idCountry=276&idBranch=0&revenueFrom=-1000000000000000000&revenueTo=1000000000000000000&employeesFrom=0&employeesTo=100000000&sortMethod=revenueDesc&p=4

使用此代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
import time 

driver = webdriver.Chrome('/Users/rieder/Anaconda3/chromedriver_win32/chromedriver.exe')

driver.get('https://de.statista.com/companydb/suche?idCountry=276&idBranch=0&revenueFrom=-1000000000000000000&revenueTo=1000000000000000000&employeesFrom=500&employeesTo=100000000&sortMethod=revenueDesc&p=1')

driver.find_element_by_id("cookiesNotificationConfirm").click();

company_name = driver.find_element_by_class_name('zebraTable__td zebraTable__td--companyName')

print(company_name)

我尝试了4个小时,但仍无法获取。我用xpath,链接文本等不同的方法进行了尝试,但是我得到的只是一个空公司名称,例如“ []”。

有人知道硒如何找到“Liebherr-HausgeräteOchsenhausen GmbH”的确切文本吗?

非常感谢。

DebanjanB

要打印文本Schneider Electric Holding Germany GmbH,您必须为引入WebDriverWaitvisibility_of_element_located()并且可以使用以下两种定位策略之一

  • 使用CSS_SELECTOR文字属性:

    driver.get('https://de.statista.com/companydb/suche?idCountry=276&idBranch=0&revenueFrom=-1000000000000000000&revenueTo=1000000000000000000&employeesFrom=0&employeesTo=100000000&sortMethod=revenueDesc&p=4')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#cookiesNotificationConfirm"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "table.zebraTable.zebraTable--companies tr:nth-child(2)>td.zebraTable__td.zebraTable__td--companyName>a"))).text)
    
  • 使用XPATHget_attribute("innerHTML")

    driver.get('https://de.statista.com/companydb/suche?idCountry=276&idBranch=0&revenueFrom=-1000000000000000000&revenueTo=1000000000000000000&employeesFrom=0&employeesTo=100000000&sortMethod=revenueDesc&p=4')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='cookiesNotificationConfirm']"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@class='zebraTable zebraTable--companies']//following::tr[2]/td[@class='zebraTable__td zebraTable__td--companyName']/a"))).get_attribute("innerHTML"))
    
  • 控制台输出:

    Schneider Electric Holding Germany GmbH
    
  • 注意:您必须添加以下导入:

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

您可以在如何使用Selenium检索WebElement的文本中找到相关的讨论-Python


其他

链接到有用的文档:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python / BeautifulSoup / Selenium 网页抓取 - 无法查看内容

使用Python / Selenium抓取网页的内容

AttributeError - 网页抓取 - Python - Selenium

在 Python 中使用 Selenium 进行网页抓取

使用 selenium python 从网页中抓取图像?

从网页上抓取数据-Python / Selenium

Python Selenium,抓取网页javascript表

在 python 中使用 selenium 抓取 HTML 代码部分(不是文本)

使用Selenium抓取网页时缺少HTML内容

Python请求/ URLlib / Selenium无法解析整个网页的HTML

Python Selenium Web抓取

无法在使用 selenium 和 python 的网页抓取期间识别唯一元素

Python / Selenium-无法打印所有段落的文本内容

Selenium无法点击网页python的按钮

使用 python selenium 抓取 HTML 表格

robots.txt 内容 / selenium 网页抓取

将网页抓取的表格放入Excel(Selenium,Python)

使用 Python Selenium 性能进行网页抓取

使用Selenium Python(NSFW)从网页中抓取URL

如何使用Selenium(Python)网页抓取多个页面

如何使用 Selenium 和 Python 从 youtube 上抓取网页

即使在使用 selenium python 抓取数据之前网页也已关闭

在 python 网页抓取中使用 Selenium 对 BeautifulSoup 进行分页

使用 Selenium&gChrome 进行 Python 网页抓取

使用 Selenium 进行 Python 抓取

Python Selenium 来抓取 USPS

Python + Selenium + Web抓取缓慢

Selenium Python中的Web抓取

如何在Selenium(Python)的网页的特定部分中搜索文本?有图片: