mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2024-04-21 12:31:36 +00:00
52 lines
870 B
Go
52 lines
870 B
Go
package goquery
|
|
|
|
import (
|
|
"exp/html"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type Document struct {
|
|
Root *html.Node
|
|
Url *url.URL
|
|
}
|
|
|
|
func NewDocumentFromNode(root *html.Node) (d *Document) {
|
|
// Create and fill the document
|
|
d = &Document{root, nil}
|
|
return
|
|
}
|
|
|
|
func NewDocument(url string) (d *Document, e error) {
|
|
// Load the URL
|
|
res, e := http.Get(url)
|
|
if e != nil {
|
|
return
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
// Parse the HTML into nodes
|
|
root, e := html.Parse(res.Body)
|
|
if e != nil {
|
|
return
|
|
}
|
|
|
|
// Create and fill the document
|
|
d = &Document{root, res.Request.URL}
|
|
return
|
|
}
|
|
|
|
type Selection struct {
|
|
Nodes []*html.Node
|
|
document *Document
|
|
prevSel *Selection
|
|
}
|
|
|
|
func newEmptySelection(doc *Document) *Selection {
|
|
return &Selection{nil, doc, nil}
|
|
}
|
|
|
|
func newSingleSelection(node *html.Node, doc *Document) *Selection {
|
|
return &Selection{[]*html.Node{node}, doc, nil}
|
|
}
|