Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andybalholm/cascadia
CSS selector library in Go
https://github.com/andybalholm/cascadia
Last synced: about 1 month ago
JSON representation
CSS selector library in Go
- Host: GitHub
- URL: https://github.com/andybalholm/cascadia
- Owner: andybalholm
- License: bsd-2-clause
- Created: 2015-03-28T00:45:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-27T12:39:35.000Z (over 1 year ago)
- Last Synced: 2024-10-29T19:59:00.258Z (about 1 month ago)
- Language: Go
- Homepage:
- Size: 84 KB
- Stars: 698
- Watchers: 18
- Forks: 65
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - cascadia - CSS selectors Go library (HTML utilities)
- go-awesome - cascadia - CSS selectors (Open source library / Word Processing)
README
# cascadia
[![](https://travis-ci.org/andybalholm/cascadia.svg)](https://travis-ci.org/andybalholm/cascadia)
The Cascadia package implements CSS selectors for use with the parse trees produced by the html package.
To test CSS selectors without writing Go code, check out [cascadia](https://github.com/suntong/cascadia) the command line tool, a thin wrapper around this package.
[Refer to godoc here](https://godoc.org/github.com/andybalholm/cascadia).
## Example
The following is an example of how you can use Cascadia.
```go
package mainimport (
"fmt"
"log"
"strings""github.com/andybalholm/cascadia"
"golang.org/x/net/html"
)var pricingHtml string = `
`func Query(n *html.Node, query string) *html.Node {
sel, err := cascadia.Parse(query)
if err != nil {
return &html.Node{}
}
return cascadia.Query(n, sel)
}func QueryAll(n *html.Node, query string) []*html.Node {
sel, err := cascadia.Parse(query)
if err != nil {
return []*html.Node{}
}
return cascadia.QueryAll(n, sel)
}func AttrOr(n *html.Node, attrName, or string) string {
for _, a := range n.Attr {
if a.Key == attrName {
return a.Val
}
}
return or
}func main() {
doc, err := html.Parse(strings.NewReader(pricingHtml))
if err != nil {
log.Fatal(err)
}
fmt.Printf("List of pricing plans:\n\n")
for i, p := range QueryAll(doc, "div.card.mb-4.box-shadow") {
planName := Query(p, "h4").FirstChild.Data
price := Query(p, ".pricing-card-title").FirstChild.Data
usersIncluded := Query(p, "li:first-child").FirstChild.Data
storage := Query(p, "li:nth-child(2)").FirstChild.Data
detailsUrl := AttrOr(Query(p, "li:last-child a"), "href", "(No link available)")
fmt.Printf(
"Plan #%d\nName: %s\nPrice: %s\nUsers: %s\nStorage: %s\nDetails: %s\n\n",
i+1,
planName,
price,
usersIncluded,
storage,
detailsUrl,
)
}
}
```
The output is:
```
List of pricing plans:Plan #1
Name: Free
Price: $0/mo
Users: 10 users included
Storage: 2 GB of storage
Details: https://example.comPlan #2
Name: Pro
Price: $15/mo
Users: 20 users included
Storage: 10 GB of storage
Details: https://example.comPlan #3
Name: Enterprise
Price: $29/mo
Users: 30 users included
Storage: 15 GB of storage
Details: (No link available)
```