简体   繁体   English

如何使用 Selenium (Python) 从时间标签中提取文本

[英]How to extract text from time tag with Selenium (Python)

I tried to extract all text from a 'time' tag.我试图从“时间”标签中提取所有文本。 The HTML is from this page : https://www.python.org HTML 来自此页面: https : //www.python.org

Here is my code:这是我的代码:

event_times = driver.find_elements(By.CSS_SELECTOR, value=".event-widget time")
    
for time in event_times:
    print(time.get_attribute("innerHTML"))

I have this output:我有这个输出:

<span class="say-no-more">2021-</span>11-13
<span class="say-no-more">2021-</span>11-15
<span class="say-no-more">2021-</span>11-18
<span class="say-no-more">2021-</span>11-19
<span class="say-no-more">2021-</span>11-24

If I change to :如果我改为:

for time in event_times:    
    print(time.text)

The output:输出:

11-13
11-15
11-18
11-19
11-24

My question : is there a direct way to display all the text contained in the time tag, namely 2021-11-13 , 2021-11-15 , etc...?我的问题:有没有一种直接的方法来显示时间标签中包含的所有文本,即2021-11-132021-11-15等......?

Except to separate into two searches (in span for "year" and in time for "month-day"), I don't see how to do this...除了分成两个搜索(跨度为“年”和时间为“月-日”),我不知道如何做到这一点......

To extract all the texts from the <time> tags eg 2021-11-05 , using Selenium and you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies :要从<time>标签中提取所有文本,例如2021-11-05 ,使用Selenium您必须为visibility_of_all_elements_located()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "ul.menu>li time")))])
  • Using XPATH :使用XPATH

     print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//ul[@class='menu']/li//time")))])
  • Console Output:控制台输出:

     ['2021-11-05', '2021-11-02', '2021-10-26', '2021-10-19', '2021-10-18', '2021-11-13', '2021-11-15', '2021-11-18', '2021-11-19', '2021-11-24']
  • Note : You have to add the following imports :注意:您必须添加以下导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium 和 Python 从 span 标签内的文本节点中提取文本 121.6 - How to extract the text 121.6 from the text node within the span tag using Selenium and Python 如何使用 Python 中的 Selenium 提取 li 标签内的文本 - How to extract text inside a li tag using Selenium in Python Python Selenium - 如何根据 span 标签内的文本提取元素? - Python Selenium - How to extract element based on text inside span tag? 使用Python和Selenium,如何从包含以下内容的HTML中提取文本: <p> 标签? - Using Python & Selenium, how to extract the text from HTML containing the <p> tag? 如何<a>通过Python使用Selenium</a>从<a>标记中</a>提取所有文本 - How to extract all the texts from <a> tag using Selenium through Python 如何从 selenium python 中提取 href 标签元素? - how to extract a href tag elemnt from selenium python? Python Selenium:如何从 iframe 中的元素中提取文本? - Python Selenium: How to extract text from an element within an iframe? 如何使用 Selenium 和 Python 从 HTML 中提取文本 - How to extract the text from the HTML using Selenium and Python 如何使用 Selenium 和 Python 从 html 中提取文本 H MATTHEWS - How to extract the text H MATTHEWS from the html using Selenium and Python 如何使用Selenium Python从duckduckgo的搜索结果中提取文本 - How to extract the text from the search results of duckduckgo using Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM