causal-agent / scraper

HTML parsing and querying with CSS selectors
https://docs.rs/scraper
ISC License
1.79k stars 98 forks source link

How to select contains and start with? #163

Closed newtome8888 closed 6 months ago

newtome8888 commented 6 months ago

Suppose there is a html fragment: <frag> <div class="a"></div> <div class="id_1"></div> <div class="id_2"></div> <div class="id_3"></div> <span class="aaa"></span> <span class="bbb"></span> </frag>

Qustion: 1.How to select all the nodes with class name have prefix "id_"? 2.How to select all the nodes with class name contains character "a"?

adamreichold commented 6 months ago

Instead of using the short-hand for classes, directly select for the class attribute, e.g.

div[class^='id_']
div[class*='a']

See https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors for details.