Filters up to this point have been specific to element nodes, but they also exist for attribute nodes. Attribute filters are surrounded by square brackets in both CSS and XPath, but differ in that CSS uses mostly operators for conditions while XPath uses mostly functions. Unlike other filters described in this chapter, support for attribute filters is fairly universal between different libraries.
- [href] matches all nodes that have an attribute node with the name href.
- [href=''/home"] matches all nodes with an attribute node named href that has a value of “/home".
- [href!=“/home"] matches all nodes with an attribute node named href that do not have a value of “/home".
- [href~=“/"] matches all nodes with an attribute node named href and have a value that starts with “/".
- [href$=“-us"] matches all nodes with an attribute node named href and have a value that ends with “-us".
- [href*=“-us"] matches all nodes with an attribute node named href and have a value that contains “-us" anywhere within the value.
- [src*=“ad"][alt"=“Advertisement"] matches all nodes that have both an attribute node named src with a value containing “ad" and an attribute node named alt withavalue startingwith “Advertisement".
Selector
|
CSS
|
XPath
|
has attribute
|
[href]
|
//*[@href]
|
has attribute value
|
[href=“/home"]
|
//*[@href=“/home"]
|
has different attribute value
|
[href!=“/home"]
|
//*[@href!=“/home"]
|
has attribute value starting with substring
|
[href~=“/"]
|
//*[starts-with(@href,
“/")]
|
has attribute value ending with substring
|
[href$=“-us"]
|
//*[ends-width(@href,
“-us")]
|
has attribute value containing substring
|
[href*=“-us"]
|
//*[contains(@href,
“-us")]
|
multiple attribute filters
|
[src*=“ad"][alt~=“Advertisement"]
|
//*[contains(@src, “ad") and starts-with(@alt, “Advertisement")
|
© CSS Selector Libraries — Web Scraping
>>> Back to TABLE OF CONTENTS <<< |