如何获取标签下的文本

2024-04-20 00:05:42 发布

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

我在试着在标签下面找到文字 enter image description here

我尝试了几种不同的选择:

dneyot=driver.find_elements_by_xpath("//*[starts-with(@id, 'popover-')]/text()")
dneyot=driver.find_elements_by_xpath("//*[starts-with(@id, 'popover-')]/b[1]/text()")

我的代码:

^{pr2}$

升级版: 我使用以下工具在浏览器中找到我需要的项目:

//*[starts-with(@id, 'popover-')]/text()[1]

但会出错

    selenium.common.exceptions.InvalidSelectorException:
Message: invalid selector: The result of the xpath expression "//*[starts-with(@id, 'popover-')]/text()[1]" is: [object Text]. It should be an element.

Tags: 代码textidbydriverwith标签elements
3条回答

如果要获取不包括<b>节点文本的文本,则需要使用以下XPath:

//div[starts-with(@id, 'popover-')]

它将标识div节点,然后通过使用find_elements_by_xpath()方法,您可以从div node检索所有文本。请尝试以下代码:

^{pr2}$

更新:

我怀疑,上述方法可能不起作用,我们可能无法使用常规方法识别/获取数据-在这种情况下,您需要使用JavaScriptExecutor来获取如下数据:

driver = webdriver.Chrome('chromedriver.exe')
driver.get("file:///C:/NotBackedUp/SomeHTML.html")

xPath = "//div[starts-with(@id, 'popover-')]"
elements = driver.find_elements_by_xpath(xPath)
for element in elements:
    lenght = int(driver.execute_script("return arguments[0].childNodes.length;", element));
    for i in range(1, lenght + 1, 1):
        try:
            data = str(driver.execute_script("return arguments[0].childNodes["+str(i)+"].textContent;", element)).strip();
            if data != None and data != '':
                print data
        except:
            print "=> Can't print some data..."

由于您的网站是用英语以外的其他语言编写的,您可能无法打印/获取一些数据。在

要获取特定的子节点数据,需要执行以下操作:

^{4}$

我希望它能帮助。。。在

find_elements_by_xpath()返回一个webelement-selenium实际使用的基本对象。
xpath以/text()结尾,它将返回xml文档中节点的文本内容,而不是selenium期望的对象。因此,只需将其更改为不具有后缀-这将返回元素本身,并通过在Python中调用.text获得其(元素的)文本:

dneyot=driver.find_elements_by_xpath("//*[starts-with(@id, 'popover-')]")
for element in dneyot:
    print("Период показов >3 дней", element.text)

使用Beautifulsoup:

在父项div内找到div,其中包含{}。在

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.your_url_here.com/")

soup = BeautifulSoup(page.content, 'html.parser')
data = soup.find("div", {"id": "popover-34252127"})
print(data)

相关问题 更多 >