-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
130 lines (101 loc) · 2.71 KB
/
Copy pathreader.go
File metadata and controls
130 lines (101 loc) · 2.71 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package reader
import (
"time"
"github.com/sclevine/agouti"
"github.com/yosssi/gohtml"
gentleman "gopkg.in/h2non/gentleman.v2"
"gopkg.in/h2non/gentleman.v2/plugins/headers"
"gopkg.in/h2non/gentleman.v2/plugins/redirect"
"gopkg.in/h2non/gentleman.v2/plugins/timeout"
)
// Config Struct...
// 系统配置
var Config = struct {
Timeout time.Duration
Browser string
Platform string
}{
Timeout: 10,
Browser: "chrome",
Platform: "MAC",
}
// Load Function
// 加载Web ...
func Load(url string, navigate ...bool) (*Document, bool) {
doc := &Document{}
doc.URL = url
if len(navigate) == 1 && navigate[0] {
if doc, ok := doc.NavigateHTTPClient(); ok {
return doc, ok
}
} else {
if doc, ok := doc.TerminalHTTPClient(); ok {
return doc, ok
}
}
return nil, false
}
// UTF8 Funciton
// 转换为UTF8 ...
func (doc *Document) UTF8() (*Document, bool) {
return doc.Converter()
}
// TerminalHTTPClient Funciton
// 命令行下HTTP客户端 ...
func (doc *Document) TerminalHTTPClient() (*Document, bool) {
cli := gentleman.New()
cli.URL(doc.URL)
req := cli.Request()
// 可根据自己需求进行调整头部信息。
req.Use(headers.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"))
req.Use(headers.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"))
req.Use(headers.Set("Accept-Language", "zh-CN,zh;q=0.9"))
req.Use(timeout.Request(10 * time.Second))
req.Use(timeout.Request(10 * time.Second))
req.Use(redirect.Limit(20))
// Define dial specific timeouts
req.Use(timeout.Dial(5*time.Second, 30*time.Second))
resp, err := req.Send()
doc.Resp = resp
if err != nil && !resp.Ok {
return doc, false
}
if resp.StatusCode == 200 {
doc.HTML = resp.String()
doc.Bytes = resp.Bytes()
return doc, true
}
return doc, false
}
// NavigateHTTPClient Function
// 系统桌面HTTP客户端 ...
func (doc *Document) NavigateHTTPClient() (*Document, bool) {
for {
select {
case <-time.After(Config.Timeout * time.Second):
return nil, false
case <-time.Tick(2 * time.Millisecond):
driver := agouti.PhantomJS()
defer driver.Stop()
if err := driver.Start(); err != nil {
return nil, false
}
capabilities := agouti.
NewCapabilities().
Browser(Config.Browser).
Platform(Config.Platform).
Without("javascriptEnabled")
page, err := driver.NewPage(agouti.Desired(capabilities))
if err != nil {
return nil, false
}
page.Navigate(doc.URL)
if doc.HTML, err = page.HTML(); err == nil {
doc.HTML = gohtml.Format(doc.HTML)
return doc, true
}
}
}
}
/* End of file http.go */
/* Location: ./http.go */