Selenium如何提取href并标记名称Python?

2024-09-24 22:19:04 发布

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

我正试图从中提取href和数据名

URL: https://www2.deloitte.com/global/en/pages/about-deloitte/topics/combating-covid-19-with-resilience.html?icid=covid-19_article-nav

enter image description here

我尝试了下面的代码,但只能在类“promo focus”下提取href,但我还想从data-promoname中获取COVID-19 Economic cases: Scenarios for business leaders

driver = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
url = "https://www2.deloitte.com/global/en/pages/about-deloitte/topics/combating-covid-19-with-resilience.html?icid=covid-19_article-nav"
driver.get(url)

for i in driver.find_elements_by_class_name('promo-focus'):
    print(i.get_attribute('href'))

有人能告诉我如何使用Python实现这一点吗


Tags: httpscomdriverwithpagesglobalenabout
2条回答

要从数据名中获取值,可以使用获取属性方法。此方法可用于获取与其标记对应的任何属性的值

driver_path = 'C:/chromedriver.exe'  #the path to your chrome driver
browser = webdriver.Chrome(driver_path)
url_to_open = 'https://www2.deloitte.com/global/en/pages/about-deloitte/topics/combating-covid-19-with-resilience.html?icid=covid-19_article-nav'
browser.get(url_to_open)
for a in browser.find_elements_by_class_name('promo-focus'):
    print(a.get_attribute('href'))
    print(a.get_attribute("data-promoname"))

如果要查找页面上锚定标记下显示的内容,可以使用.text

print(a.text)

尝试使用text方法获取文本

示例

from selenium import webdriver

chrome_browser = webdriver.Chrome()
url = "https://www2.deloitte.com/global/en/pages/about-deloitte/topics/combating-covid-19-with-resilience.html?icid=covid-19_article-nav"
chrome_browser.get(url)
for a in chrome_browser.find_elements_by_class_name('promo-focus'):
    print(a.get_attribute('href'))
    print(a.text)

enter image description here

相关问题 更多 >