Skip to content

Commit 6392c0e

Browse files
author
bajins
committed
allows 优化
1 parent 66b55c0 commit 6392c0e

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

reptile/Netsarang.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func SendMail(mail, product string) error {
5353
// 定义变量,用来保存爬虫的数据
5454
var res string
5555

56-
err := Apply(clickSubmitMail(url, mail, &res))
56+
err := ApplyDebug(clickSubmitMail(url, mail, &res))
5757
if err != nil {
5858
return err
5959
}
@@ -136,6 +136,7 @@ func DownloadNetsarang(product string) (string, error) {
136136
}
137137
listLen := len(list)
138138
if listLen == 0 {
139+
log.Println(list)
139140
return "", errors.New("没有邮件")
140141
}
141142
mailbox := list[listLen-1]["mailbox"].(string)
@@ -169,7 +170,7 @@ func DownloadNetsarang(product string) (string, error) {
169170

170171
var attributes map[string]string
171172

172-
err = Apply(getDownloadUrl(tokenHtml.Text(), &attributes))
173+
err = ApplyDebug(getDownloadUrl(tokenHtml.Text(), &attributes))
173174
if err != nil {
174175
return "", err
175176
}

reptile/chromedp.go

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,80 @@ import (
2424
"time"
2525
)
2626

27+
const UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"
28+
2729
// 启动
2830
// context.Context部分不能抽离,否则会报 context canceled
29-
func Apply(actions ...chromedp.Action) error {
31+
func Apply(actions chromedp.Action, opts ...chromedp.ExecAllocatorOption) error {
32+
33+
//dir, err := ioutil.TempDir("", "chromedp-example")
34+
//if err != nil {
35+
// panic(err)
36+
//}
37+
//defer os.RemoveAll(dir)
38+
39+
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
40+
// 关闭chrome实例
41+
defer cancel()
42+
43+
// 自定义记录器
44+
ctx, cancel = chromedp.NewContext(ctx, chromedp.WithLogf(log.Printf))
45+
// 释放所有资源,并等待释放结束
46+
defer cancel()
47+
48+
// 设置超时时间
49+
ctx, cancel = context.WithTimeout(ctx, 3*time.Minute)
50+
// 超时关闭chrome实例
51+
defer cancel()
52+
53+
// listen network event
54+
//listenForNetworkEvent(ctx)
55+
56+
return chromedp.Run(ctx, actions)
57+
}
58+
59+
func ApplyDebug(actions chromedp.Action) error {
3060

3161
//dir, err := ioutil.TempDir("", "chromedp-example")
3262
//if err != nil {
3363
// panic(err)
3464
//}
3565
//defer os.RemoveAll(dir)
3666

37-
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36 "
67+
opts := append(chromedp.DefaultExecAllocatorOptions[:],
68+
// 设置UA,防止有些页面识别headless模式
69+
chromedp.UserAgent(UserAgent),
70+
// 窗口最大化
71+
chromedp.Flag("start-maximized", true),
72+
)
73+
return Apply(actions, opts...)
74+
}
75+
76+
func ApplyRun(actions chromedp.Action) error {
77+
78+
//dir, err := ioutil.TempDir("", "chromedp-example")
79+
//if err != nil {
80+
// panic(err)
81+
//}
82+
//defer os.RemoveAll(dir)
3883

3984
opts := append(chromedp.DefaultExecAllocatorOptions[:],
4085
chromedp.NoDefaultBrowserCheck,
4186
// 无头模式
42-
//chromedp.Flag("headless", false),
43-
chromedp.Headless,
87+
chromedp.Flag("headless", false),
88+
//chromedp.Headless,
4489
// 禁用GPU,不显示GUI
45-
chromedp.DisableGPU,
90+
//chromedp.DisableGPU,
4691
// 隐身模式启动
4792
chromedp.Flag("incognito", true),
4893
// 取消沙盒模式
4994
chromedp.NoSandbox,
5095
// 忽略证书错误
5196
chromedp.Flag("ignore-certificate-errors", true),
5297
// 指定浏览器分辨率
53-
chromedp.WindowSize(1600, 900),
98+
//chromedp.WindowSize(1600, 900),
99+
// 窗口最大化
100+
chromedp.Flag("start-maximized", true),
54101
//
55102
chromedp.Flag("in-process-plugins", true),
56103
// 不加载图片, 提升速度
@@ -60,30 +107,12 @@ func Apply(actions ...chromedp.Action) error {
60107
// 隐藏滚动条, 应对一些特殊页面
61108
//chromedp.Flag("hide-scrollbars", true),
62109
// 设置UA,防止有些页面识别headless模式
63-
chromedp.UserAgent(userAgent),
110+
chromedp.UserAgent(UserAgent),
64111
// 设置用户数据目录
65112
//chromedp.UserDataDir(dir),
66113
//chromedp.ExecPath("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"),
67114
)
68-
69-
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
70-
// 关闭chrome实例
71-
defer cancel()
72-
73-
// 自定义记录器
74-
ctx, cancel = chromedp.NewContext(ctx, chromedp.WithLogf(log.Printf))
75-
// 释放所有资源,并等待释放结束
76-
defer cancel()
77-
78-
// 设置超时时间
79-
ctx, cancel = context.WithTimeout(ctx, 3*time.Minute)
80-
// 超时关闭chrome实例
81-
defer cancel()
82-
83-
// listen network event
84-
//listenForNetworkEvent(ctx)
85-
86-
return chromedp.Run(ctx, actions...)
115+
return Apply(actions, opts...)
87116
}
88117

89118
//监听

0 commit comments

Comments
 (0)