GraphQuery - Powerful html/xml query language(三)
;) |
Use CSS selector to select elements |
json |
json(JSONSelector) |
json("title") |
Use json path to select elements |
xpath |
xpath(XpathSelector) |
xpath("//title") |
Use Xpath selector to select elements |
regex |
regex(RegexSelector) |
regex("(.*?)") |
Use Regex selector to select elements |
trim |
trim() |
trim() |
Clear spaces and line breaks before and after the string |
template |
template(TemplateStr) |
template("[{$}]") |
Add characters before and after variables |
attr |
attr(AttributeName) |
attr("lang") |
Extract the property of the current node |
eq |
eq(Index) |
eq("0") |
Take the nth element in the current node collection |
string |
string() |
string() |
Extract the current node native string |
text |
text() |
text() |
Extract the text of the current node |
link |
link(KeyName) |
link("title") |
Returns the current text of the specified key |
replace |
replace(A, B) |
replace("a", "b") |
Replace all A in the current node to B |
More detailed introduction to pipeline and function, please go to docs.
Install
GraphQuery is currently only native to Golang, but for other languages, it can be invoked as a service.
1. Golang:
go get github.com/storyicon/graphquery
Create a new go file :
package main
import (
"encoding/json"
"log"
"github.com/storyicon/graphquery"
)
func main() {
document := `
<html>
<body>
<a href="01.html">Page 1</a>
<a href="02.html">Page 2</a>
<a href="03.html">Page 3</a>
</body>
</html>
`
expr := "{ anchor `css(\"a\")` [ content `text()` ] }"
response := graphquery.ParseFromString(document, expr)
bytes, _ := json.Marshal(response.Data)
log.Println(string(bytes))
}
Run the go file, the output is as follows :
{"anchor":["Page 1","Page 2","Page 3"]}
2. Other language
We use the HTTP protocol to provide a cross-language solution for developers to query GraphQuery using any back-end language you want to use to access the specified port after starting the service.
GraphQuery-http : Cross language solution for GraphQuery
You can also use RPC for communication, but currently you may need to do this yourself, because the RPC project on GraphQuery is still under development. At the same time, We welcome the contributors to write native support code for other languages ??in GraphQuery.
|