【发布时间】:2014-03-16 00:00:33
【问题描述】:
我想从 Splinter 中的 <a> 元素中获取 href 值。
有什么api方法吗?
【问题讨论】:
我想从 Splinter 中的 <a> 元素中获取 href 值。
有什么api方法吗?
【问题讨论】:
#simplest possible working example demonstrating this in action
import splinter
b = splinter.Browser()
b.visit("http://www.google.com")
elems = b.find_by_tag("a")
for e in elems:
print(e["href"])
【讨论】:
如果您使用find_by_* methods 选择元素,则这些返回的实例为ElementLists。选择您感兴趣的元素(很可能是ElementAPI 实例)后,像字典一样访问该属性:
the_element['href']
【讨论】:
element['href'] 返回一个绝对 URL(至少在 Firefox 下),而不是实际属性,如 HTML 中的,可能是相对的。