mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-04 08:26:37 +08:00
38 lines
570 B
Go
38 lines
570 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
|
|
}
|