$、^和*符号在Python2.7和BS4中的作用

2024-10-08 18:27:13 发布

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

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>]

Tags: comidhttpexample运算符sisterselectclass
2条回答

它们是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元素。你知道吗

相关问题 更多 >

    热门问题