查找XPath段落(跟随同级?)

2024-04-25 13:17:43 发布

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

我将SeleniumPython 2.7.10一起使用,并希望抓取本页"Description"标题后面的段落:http://etfdb.com/etf/ROBO/

from selenium import webdriver as driver
from selenium.common.exceptions import NoSuchElementException

def scrape(driver, key):
    try:
        find_value = driver.find_element_by_xpath("//span[@class='panel__sub-heading' and . = '%s']/following-sibling::p" % key).text
    except NoSuchElementException:
        print "Not Found"
        return None
    else:
        value = re.search(r"(.+)", find_value).group().encode("utf-8")
        print value
        return value

description = scrape(driver, "Description")

我使用的XPath不正确,因为它不会产生任何结果。找到标题"Description"后面的段落的正确方法是什么?你知道吗


Tags: keyfromimport标题returnvaluedriverselenium