如何使用带 Python 的 Selenium 获取跨度内标签内的文本?
How do I obtain the text inside a label which is inside a span using Selenium with Python?
我想在(用于网络抓取)上使用 Selenium(带有 Python)的网站上的一段代码如下所示 -
<div class="exp_date">
<span class="uppr_sec">
<i class="exp_clndr"></i>
<label> 04 Jan 2021 09:30 AM - 04 Jan 2021 10:30 AM </label>
</span>
<br>
<div class="clear"></div>
<span class="lwr_sec">
<i class></i>
<label>Hosted By Some Random Person</label>
</span>
</div>
我想在两个范围内打印 <label>
标签中包含的文本,即“2021 年 1 月 4 日 09:30 上午 - 2021 年 1 月 4 日 10:30 上午”和“在 Python 控制台中由某个随机的人主持”,使用 Selenium。但是,我不确定这样做的步骤,因为标签嵌套在它们各自的跨度中,这些跨度嵌套在 div.
有人可以帮我解决需要的代码吗? (在 Python)
提取并打印文本,例如2021 年 1 月 4 日 09:30 上午 - 2021 年 1 月 4 日 10:30 上午 使用 and python you can use either of the following :
使用 css_selector
和 get_attribute("innerHTML")
:
print([my_elem.get_attribute("innerHTML") for my_elem in driver.find_elements_by_css_selector("div.exp_date > span.uppr_sec label")])
使用 xpath
和 text 属性:
print([my_elem.text for my_elem in driver.find_elements_by_xpath("//div[@class='exp_date']/span[@class='uppr_sec']//label")])
理想情况下你需要诱导 for visibility_of_all_elements_located()
and you can use either of the following :
使用 CSS_SELECTOR
和 get_attribute("innerHTML")
:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.exp_date > span.uppr_sec label")))])
使用 XPATH
和 text 属性:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='exp_date']/span[@class='uppr_sec']//label")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
结尾
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium
我想在(用于网络抓取)上使用 Selenium(带有 Python)的网站上的一段代码如下所示 -
<div class="exp_date">
<span class="uppr_sec">
<i class="exp_clndr"></i>
<label> 04 Jan 2021 09:30 AM - 04 Jan 2021 10:30 AM </label>
</span>
<br>
<div class="clear"></div>
<span class="lwr_sec">
<i class></i>
<label>Hosted By Some Random Person</label>
</span>
</div>
我想在两个范围内打印 <label>
标签中包含的文本,即“2021 年 1 月 4 日 09:30 上午 - 2021 年 1 月 4 日 10:30 上午”和“在 Python 控制台中由某个随机的人主持”,使用 Selenium。但是,我不确定这样做的步骤,因为标签嵌套在它们各自的跨度中,这些跨度嵌套在 div.
有人可以帮我解决需要的代码吗? (在 Python)
提取并打印文本,例如2021 年 1 月 4 日 09:30 上午 - 2021 年 1 月 4 日 10:30 上午 使用
使用
css_selector
和get_attribute("innerHTML")
:print([my_elem.get_attribute("innerHTML") for my_elem in driver.find_elements_by_css_selector("div.exp_date > span.uppr_sec label")])
使用
xpath
和 text 属性:print([my_elem.text for my_elem in driver.find_elements_by_xpath("//div[@class='exp_date']/span[@class='uppr_sec']//label")])
理想情况下你需要诱导 visibility_of_all_elements_located()
and you can use either of the following
使用
CSS_SELECTOR
和get_attribute("innerHTML")
:print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.exp_date > span.uppr_sec label")))])
使用
XPATH
和 text 属性:print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='exp_date']/span[@class='uppr_sec']//label")))])
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
结尾
Link 到有用的文档:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性returnsThe text of the element.
- Difference between text and innerHTML using Selenium