Skip to content

Commit c902867

Browse files
committed
up 2022-10-05
1 parent 0c8be1b commit c902867

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

lib/crawlergo/mychromedp.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/chromedp/cdproto/fetch"
77
"github.com/chromedp/cdproto/network"
88
"github.com/chromedp/chromedp"
9-
"github.com/chromedp/chromedp/device"
109
"github.com/chromedp/chromedp/kb"
1110
"io/ioutil"
1211
"log"
@@ -83,7 +82,7 @@ func (r *MyChromedp) fnInit() {
8382
chromedp.NoFirstRun,
8483
chromedp.NoDefaultBrowserCheck,
8584
chromedp.NoSandbox,
86-
chromedp.Headless,
85+
//chromedp.Headless,
8786
chromedp.Flag("disable-default-apps", true),
8887
chromedp.Flag("disable-extensions", true),
8988
chromedp.Flag("disable-popup-blocking", true),
@@ -109,13 +108,13 @@ func (r *MyChromedp) Setheaders(headers *map[string]interface{}) *[]chromedp.Act
109108
//chromedp.Text(`#result`, res, chromedp.ByID, chromedp.NodeVisible),
110109
}
111110

112-
func (r *MyChromedp) DoUrl(szUrl string, head *map[string]interface{}, timeout *time.Duration) error {
113-
err, _ := r.DoUrlWithFlg(szUrl, head, timeout, true)
111+
func (r *MyChromedp) DoUrl(szUrl string, head *map[string]interface{}, timeout *time.Duration, fnSend func() *chromedp.Tasks) error {
112+
err, _ := r.DoUrlWithFlg(szUrl, head, timeout, true, fnSend)
114113
return err
115114
}
116115

117116
// 启动一个tab运行url
118-
func (r *MyChromedp) DoUrlWithFlg(szUrl string, head *map[string]interface{}, timeout *time.Duration, bAutoClose bool) (err error, cancel context.CancelFunc) {
117+
func (r *MyChromedp) DoUrlWithFlg(szUrl string, head *map[string]interface{}, timeout *time.Duration, bAutoClose bool, fnSend func() *chromedp.Tasks) (err error, cancel context.CancelFunc) {
119118
if nil == timeout {
120119
n9 := 15 * time.Second
121120
timeout = &n9
@@ -134,15 +133,21 @@ func (r *MyChromedp) DoUrlWithFlg(szUrl string, head *map[string]interface{}, ti
134133

135134
var title string
136135
//var b1, b2 []byte
137-
a := []chromedp.Action{chromedp.Emulate(device.IPadMini), fetch.Enable()}
136+
// chromedp.Emulate(device.IPadMini), fetch.Enable()
137+
a := []chromedp.Action{chromedp.Navigate(szUrl)}
138+
if nil != fnSend {
139+
a = append(a, *fnSend()...)
140+
}
138141
a = append(a, *r.Setheaders(head)...)
139-
a = append(a, chromedp.Navigate(szUrl),
142+
a = append(a,
140143
chromedp.WaitVisible("body", chromedp.ByQuery),
141144
chromedp.Title(&title),
142145
//chromedp.Evaluate(`Object.keys(window);`, &res),
143146
//chromedp.CaptureScreenshot(&b1),
144-
chromedp.Emulate(device.Reset))
147+
//chromedp.Emulate(device.Reset),
148+
)
145149
if err := chromedp.Run(taskCtx, a...); err != nil {
150+
log.Println("chromedp.Run: ", err)
146151
return err, cancel
147152
}
148153

lib/crawlergo/test/main.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,71 @@ package main
22

33
import (
44
"context"
5+
"github.com/chromedp/chromedp"
56
pkg "github.com/hktalent/scan4all/lib/crawlergo"
67
"log"
8+
"time"
79
)
810

911
func main() {
12+
//ctx := context.Background()
13+
//x1 := pkg.GetChromedpInstace(&ctx)
14+
////n1 := 15 * time.Second
15+
//n1 := 1000 * time.Second
16+
//
17+
//// Username already used
18+
////x1.DoUrl("https://google.com", &map[string]interface{}{"cookie": "xxx"}, nil)
19+
//if err := x1.DoUrl("https://account.proton.me/signup?plan=free&billing=12&minimumCycle=12&currency=undefined&language=en", &map[string]interface{}{}, &n1, func() *chromedp.Tasks {
20+
// szPswd := ""
21+
// return &chromedp.Tasks{
22+
// chromedp.SetValue(`#email`, "hk_994", chromedp.ByID),
23+
// chromedp.SetValue(`#password`, szPswd, chromedp.ByID),
24+
// chromedp.SetValue(`#repeat-password`, szPswd, chromedp.ByID),
25+
// chromedp.Click("main > div.sign-layout-main-content > form > button", chromedp.NodeReady),
26+
// chromedp.Click("#html_element", chromedp.ByID),
27+
// }
28+
// // next:hk_994
29+
// // main > div.sign-layout-main-content > form > button
30+
// // 跳过手机号码
31+
// // /html/body/div[1]/div[3]/div/div/main/div[2]/form/button[2]
32+
//}); err != nil {
33+
// log.Println(err)
34+
//}
35+
////defer x1.Close()
36+
37+
RegGithub()
38+
}
39+
40+
// https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home
41+
func RegGithub() {
1042
ctx := context.Background()
1143
x1 := pkg.GetChromedpInstace(&ctx)
1244
//n1 := 15 * time.Second
13-
//x1.DoUrl("https://google.com", &map[string]interface{}{"cookie": "xxx"}, nil)
14-
if err := x1.DoUrl("https://www.baidu.com", &map[string]interface{}{}, nil); err != nil {
45+
n1 := 1000 * time.Second
46+
47+
if err := x1.DoUrl("https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home", &map[string]interface{}{}, &n1, func() *chromedp.Tasks {
48+
szPswd := ""
49+
return &chromedp.Tasks{
50+
chromedp.SetValue(`#email`, "[email protected]", chromedp.ByID),
51+
chromedp.Sleep(2 * time.Second),
52+
chromedp.Click("//*[@id=\"email-container\"]/div[2]/button", chromedp.NodeReady),
53+
chromedp.Sleep(2 * time.Second),
54+
chromedp.SetValue(`#password`, szPswd, chromedp.ByID),
55+
chromedp.Sleep(2 * time.Second),
56+
chromedp.Click(`document.querySelector("#password-container > div.d-flex.flex-items-center.flex-column.flex-sm-row > button")`, chromedp.ByJSPath),
57+
chromedp.Sleep(2 * time.Second),
58+
chromedp.SetValue(`#login`, "hk999999", chromedp.ByID),
59+
chromedp.Sleep(2 * time.Second),
60+
chromedp.SetValue(`#opt_in`, "n", chromedp.ByID),
61+
chromedp.Sleep(2 * time.Second),
62+
chromedp.Click(`document.querySelector("#opt-in-container > div.d-flex.flex-items-center.flex-column.flex-sm-row > button")`, chromedp.ByJSPath),
63+
}
64+
// next:hk_994
65+
// main > div.sign-layout-main-content > form > button
66+
// 跳过手机号码
67+
// /html/body/div[1]/div[3]/div/div/main/div[2]/form/button[2]
68+
}); err != nil {
1569
log.Println(err)
1670
}
17-
defer x1.Close()
71+
//defer x1.Close()
1872
}

0 commit comments

Comments
 (0)