MCPcopy Index your code
hub / github.com/antchfx/htmlquery / LoadURL

Function LoadURL

query.go:94–137  ·  view source on GitHub ↗

LoadURL loads the HTML document from the specified URL. Default enabling gzip on a HTTP request.

(url string)

Source from the content-addressed store, hash-verified

92
93// LoadURL loads the HTML document from the specified URL. Default enabling gzip on a HTTP request.
94func LoadURL(url string) (*html.Node, error) {
95 req, err := http.NewRequest("GET", url, nil)
96 if err != nil {
97 return nil, err
98 }
99 // Enable gzip compression.
100 req.Header.Add("Accept-Encoding", "gzip")
101 resp, err := http.DefaultClient.Do(req)
102 if err != nil {
103 return nil, err
104 }
105 var reader io.ReadCloser
106
107 defer func() {
108 if reader != nil {
109 reader.Close()
110 }
111 }()
112 encoding := resp.Header.Get("Content-Encoding")
113 switch encoding {
114 case "gzip":
115 gzipReader, err := gzip.NewReader(resp.Body)
116 if err != nil {
117 return nil, err
118 }
119 reader = gzipReader
120 case "deflate":
121 zlibReader, err := zlib.NewReader(resp.Body)
122 if err != nil {
123 return nil, err
124 }
125 reader = zlibReader
126 case "":
127 reader = resp.Body
128 default:
129 return nil, fmt.Errorf("%s compression is not support", encoding)
130 }
131
132 r, err := charset.NewReader(reader, resp.Header.Get("Content-Type"))
133 if err != nil {
134 return nil, err
135 }
136 return html.Parse(r)
137}
138
139// LoadDoc loads the HTML document from the specified file path.
140func LoadDoc(path string) (*html.Node, error) {

Callers 3

TestLoadURLFunction · 0.85
TestLoadURL_ErrorCasesFunction · 0.85

Calls

no outgoing calls

Tested by 3

TestLoadURLFunction · 0.68
TestLoadURL_ErrorCasesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…