在Selenium Doc中,他们在=
运算符之前使用了^
、$
和*
运算符,但没有一个解释为什么这样的特殊符号
soup.select('a[href="http://example.com/elsie"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
soup.select('a[href^="http://example.com/"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.select('a[href$="tillie"]')
# [<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.select('a[href*=".com/el"]')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
它们是substring matching attribute selectors adapted from CSS 3:
=
仅当给定值与元素的属性值相等时才匹配。你知道吗^=
仅当给定值是元素属性值的前缀时才匹配。你知道吗$=
仅当给定值是元素属性值的后缀时才匹配。你知道吗*=
仅当给定值包含在元素的属性值中时才匹配。你知道吗就你而言:
a[href="http://example.com/elsie"]
选择其href
属性值等于http://example.com/elsie
的任何a
元素。你知道吗a[href^="http://example.com/"]
选择其href
属性值以http://example.com/
开头的任何a
元素。你知道吗a[href$="tillie"]
选择其href
属性值以tillie
结尾的任何a
元素。你知道吗a[href*=".com/el"]
选择其href
属性值包含.com/el
的任何a
元素。你知道吗您看到的是CSS选择器:
http://www.w3.org/TR/css3-selectors/#selectors
相关问题 更多 >
编程相关推荐