简体   繁体   English

使用Python和Selenium,如何从包含以下内容的HTML中提取文本: <p> 标签?

[英]Using Python & Selenium, how to extract the text from HTML containing the <p> tag?

This I know is a very simple question. 我知道这是一个非常简单的问题。 I'm quite sick and trying to finish up this presentation and my brain just doesn't seem to be working right. 我很恶心,试图完成本演示文稿,但我的大脑似乎工作不正常。

The HTML code is as follows: HTML代码如下:

<p>
    <b>Postal code:</b>
    3502
</p>

The defect is the zipcode text field is only accepting four characters. 缺陷是邮政编码文本字段仅接受四个字符。 Once submitted, I'm trying to grab the number "3502" in this case and use len to count them. 提交后,在这种情况下,我尝试获取数字“ 3502”,并使用len进行计数。

The problem is that you cannot directly locate the "text" nodes with find_element_* commands in selenium - the locators you use have to point to actual elements. 问题是您无法使用硒中的find_element_*命令直接定位“文本”节点-您使用的定位器必须指向实际元素。

In this case, I would get the p element's text, split by : and get the last item: 在这种情况下,我将获取p元素的文本,并用:分割并得到最后一项:

text = driver.find_element_by_xpath("//p[b = 'Postal code:']").text
postal_code = text.split(":")[-1].strip()
print(postal_code)

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

相关问题 如何从<p>标签使用 XPath Selenium 和 Python</p> - How get the text from the <p> tag using XPath Selenium and Python 如何使用 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 从 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 如何从中提取文本<p>标签?</p> - How to extract text from <p> tag? 如何使用 Selenium (Python) 从时间标签中提取文本 - How to extract text from time tag with Selenium (Python) 如何提取使用&#39;a&#39;中的类定义的按钮的链接 <p> &#39;使用selenium在html中标记 - How Do I Extract a link of a button that is defined using a class within a '<p>' tag in html using selenium 如何使用 selenium 和 Python 根据 html 从 span 标签中提取文本 - How to extract the texts from the span tag as per the html using selenium and Python 如何<a>通过Python使用Selenium</a>从<a>标记中</a>提取所有文本 - How to extract all the texts from <a> tag using Selenium through Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM