如何使用Splinter查找元素值?

时间:2014-02-15 19:48:26

标签: python dom web-scraping screen-scraping splinter

我有以下html:

<p class="attrs"><span>foo:</span> <strong>foo</strong></p>
<p class="attrs"><span>bar:</span> <strong>bar</strong></p>
<p class="attrs"><span>foo2:</span> <strong></strong></p>
<p class="attrs"><span>description:</span> <strong>description body</strong></p>
<p class="attrs"><span>another foo:</span> <strong>foooo</strong></p>

我想使用splinter获取描述主体。我已设法使用

获取p的列表
browser.find_by_css("p.attrs")

3 个答案:

答案 0 :(得分:6)

xpath = '//p[@class="attrs"]/span[text()="description:"]/following-sibling::strong'
description = browser.find_by_xpath(xpath).first.text

答案 1 :(得分:2)

您是否可以使用find_by_tag获取说明?

按标记查找

browser.find_by_tag('span')

然后遍历所有'span'标签并查找'description'的值。我使用了文档here

答案 2 :(得分:-1)

如果您想使用硒库尝试其他方法,则可以使用以下代码完成:

import selenium
from selenium import webdriver
driver = webdriver.Chrome('PATH_LOCATION_TO_CHROME_DRIVER') 
driver.find_elements_by_class_name("attrs")

希望这会有所帮助!用chrome驱动程序的位置替换PATH_LOCATION_TO_CHROME_DRIVER ---,如果您是google,则应为要下载的第一个或第二个链接,然后将该下载内容放入Python的项目文件夹中。