-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFetchAndStorePage.go
More file actions
30 lines (24 loc) · 821 Bytes
/
FetchAndStorePage.go
File metadata and controls
30 lines (24 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main
import (
"github.com/antchfx/htmlquery"
"go.zoe.im/surferua"
"golang.org/x/net/html"
"io/ioutil"
"net/http"
)
// FetchAndStorePage fetches a web page and stores its content
func FetchAndStorePage(url string, filename string) (doc *html.Node) {
request, _ := http.NewRequest(http.MethodGet, url, nil)
request.Header.Add("Cache-Control", "no-cache")
request.Header.Add("Accept", "*/*")
request.Header.Add("Connection", "keep-alive")
request.Header.Add("User-Agent", surferua.New().Desktop().Chrome().String())
response, err := http.DefaultClient.Do(request)
LogFatalAndExitIfNotNull(err)
body, err := ioutil.ReadAll(response.Body)
LogFatalAndExitIfNotNull(err)
ioutil.WriteFile(filename, []byte(body), 0644)
doc, err = htmlquery.LoadDoc(filename)
LogFatalAndExitIfNotNull(err)
return
}