$、^和*符号在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