package goquery import ( "errors" "io" "net/http" "net/url" "github.com/andybalholm/cascadia" "golang.org/x/net/html" ) // Document represents an HTML document to be manipulated. Unlike jQuery, which // is loaded as part of a DOM document, and thus acts upon its containing // document, GoQuery doesn't know which HTML document to act upon. So it needs // to be told, and that's what the Document class is for. It holds the root // document node to manipulate, and can make selections on this document. type Document struct { *Selection Url *url.URL rootNode *html.Node } // NewDocumentFromNode is a Document constructor that takes a root html Node // as argument. func NewDocumentFromNode(root *html.Node) *Document { return newDocument(root, nil) } // NewDocument is a Document constructor that takes a string URL as argument. // It loads the specified document, parses it, and stores the root Document // node, ready to be manipulated. // // Deprecated: Use the net/http standard library package to make the request // and validate the response before calling goquery.NewDocumentFromReader // with the response's body. func NewDocument(url string) (*Document, error) { // Load the URL res, e := http.Get(url) if e != nil { return nil, e } return NewDocumentFromResponse(res) } // NewDocumentFromReader returns a Document from an io.Reader. // It returns an error as second value if the reader's data cannot be parsed // as html. It does not check if the reader is also an io.Closer, the // provided reader is never closed by this call. It is the responsibility // of the caller to close it if required. func NewDocumentFromReader(r io.Reader) (*Document, error) { root, e := html.Parse(r) if e != nil { return nil, e } return newDocument(root, nil), nil } // NewDocumentFromReaderWithOptions is like NewDocumentFromReader, with the // parse options of the html package. // // The option that usually matters is html.ParseOptionEnableScripting(false), // which makes