$、^和*符号在python 2.7和BS4中的作用

$、^和*符号在python 2.7和BS4中的作用,python,python-2.7,beautifulsoup,Python,Python 2.7,Beautifulsoup,在中,他们在下面代码中的=运算符之前使用了^、$和*:但没有解释为什么使用这些特殊符号 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

在中,他们在下面代码中的
=
运算符之前使用了
^
$
*
:但没有解释为什么使用这些特殊符号

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>]
soup.select('a[href='http://example.com/elsie"]')
# []
soup.select('a[href^='http://example.com/"]')
# [,
#  ,
#  ]
soup.select('a[href$=“tillie”]”)
# []
soup.select('a[href*=“.com/el”]”)
# []

您看到的是CSS选择器:

这些是:

  • =
    仅当给定值等于元素的属性值时匹配
  • ^=
    仅当给定值是元素属性值的前缀时匹配
  • $=
    仅当给定值是元素属性值的后缀时匹配
  • *=
    仅当给定值包含在元素的属性值中时匹配
就你而言:

  • a[href=”http://example.com/elsie“]
    选择其
    href
    属性值等于
    http://example.com/elsie
  • a[href^=”http://example.com/“]
    选择其
    href
    属性值以
    http://example.com/
  • a[href$=“tillie”]
    选择其
    href
    属性值以
    tillie
    结尾的任何
    a
    元素
  • a[href*=“.com/el”]
    选择其
    href
    属性值包含
    .com/el
    的任何
    a
    元素

我的桌面上没有打开该链接。你能在我的例子中告诉我它是做什么的吗?哦!!天哪,它开了!!明白了!谢谢你的帮助:)我能对这个问题有一些想法吗