-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path123.go
183 lines (176 loc) · 6.47 KB
/
123.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import(
"fmt"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/device"
// "github.com/chromedp/cdproto/cdp"
"context"
"io/ioutil"
"encoding/base64"
"strings"
"time"
"strconv"
"errors"
)
var url="https://kyfw.12306.cn/otn/resources/login.html"
var cpatchaURL="http://littlebigluo.qicp.net:47720"
var buyURL="https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc"
var stationMap map[string]string=map[string]string{"北京":"BJP","定州":"DXP"}
var postionRes string
func main(){
ctx,err:=Login("624867029@qq.com","m127906pA4")
if err==nil{
fmt.Println("登录成功,开始查询车票。。。")
queryAndBook(ctx,"北京","定州","2019-11-05")
}else{
fmt.Println("登录失败")
}
}
func queryAndBook(ctx1 context.Context,from,to,date string){
var useless interface{}
fmt.Println(from,"\t",to,"\t",date)
err:=chromedp.Run(ctx1,
// chromedp.EvaluateAsDevTools(`$("#J-chepiao > div").show()`,&useless),
chromedp.EvaluateAsDevTools(`document.querySelector("#J-chepiao > div").style.display="block"`,&useless),
chromedp.WaitVisible(`//li[@id="J-chepiao"]//li[a="单程"]/a`,chromedp.BySearch),
chromedp.Sleep(time.Second*3),
chromedp.Click(`//li[@id="J-chepiao"]//li[a="单程"]/a`,chromedp.BySearch),
chromedp.WaitVisible(`//form[@id="queryLeftForm"]`,chromedp.BySearch),
chromedp.EvaluateAsDevTools(`document.getElementById("fromStationText").value="`+from+`"`,&useless),
chromedp.EvaluateAsDevTools(`document.getElementById("fromStation").value="`+stationMap[from]+`"`,&useless),
chromedp.EvaluateAsDevTools(`document.getElementById("toStationText").value="`+to+`"`,&useless),
chromedp.EvaluateAsDevTools(`document.getElementById("toStation").value="`+stationMap[to]+`"`,&useless),
chromedp.EvaluateAsDevTools(`document.getElementById("train_date").value="`+date+`"`,&useless),
chromedp.Click(`//a[@id="query_ticket"]`,chromedp.BySearch),
chromedp.WaitVisible(`//div[@id="t-list"]//tbody[1]//tr`,chromedp.BySearch),
chromedp.Sleep(time.Second*60),
)
if err!=nil{
fmt.Println("查询车票出错")
fmt.Printf("%V",err)
panic(err.Error())
}
}
func Login(user,pass string)(cxtt context.Context,err error){
e_ctx,_:=chromedp.NewExecAllocator(context.Background(),chromedp.NoSandbox)
ctx,_:=chromedp.NewContext(e_ctx,chromedp.WithErrorf(func (x string,ops ...interface{}){
fmt.Println("Error:-------------------------------->")
fmt.Printf(x,ops)
fmt.Println("\n<--------------------------------------")
}),chromedp.WithDebugf(func (x string,ops ...interface{}){
for _,v:=range ops{
str:=fmt.Sprintf(x,v)
if strings.Contains(str,"www.recaptcha.net")||strings.Contains(str,`recaptcha.google.cn`)||strings.Contains(str,`www.google.com/recaptcha`){
fmt.Println(str)
fmt.Println("Warning:This site may use google's Recaptcha to stop robot,to Crwal this site you try without Headless mode.")
}
}
}),chromedp.WithLogf(func (x string,ops ...interface{}){
fmt.Println("Log:-------------------------------->")
fmt.Printf(x,ops)
fmt.Println("\n<--------------------------------------")
}))
var dataUrl string
var getDataUrl bool
var useless interface{}
err=chromedp.Run(ctx,
chromedp.Navigate(url),
chromedp.Emulate(device.Info{"PC","",1920,1080,1.0,true,false,false}),
chromedp.WaitVisible(`//ul[@class="login-hd"]`,chromedp.BySearch),
chromedp.Click(`//ul[@class="login-hd"]/li[@class="login-hd-account"]//a`,chromedp.BySearch),
chromedp.WaitVisible(`//img[@id="J-loginImg"]`,chromedp.BySearch),
chromedp.AttributeValue(`//img[@id="J-loginImg"]`,`src`,&dataUrl,&getDataUrl,chromedp.BySearch),
)
if err!=nil{
fmt.Println("请求站点错误")
panic(err.Error())
}
if getDataUrl{
fmt.Println("下载验证码。。。")
file:=`E:\EasterGitRepositorys\GoPractice\code.png`
dataUrl2Img(dataUrl,file,true)
fmt.Println("解析验证码。。。")
positions:=analyseCapatcha(file)
fmt.Println(positions)
codeHTML:=position2HTML(positions)
fmt.Println(codeHTML)
err=chromedp.Run(ctx,
chromedp.SendKeys(`//input[@id="J-userName"]`,user,chromedp.BySearch),
chromedp.SendKeys(`//input[@id="J-password"]`,pass,chromedp.BySearch),
chromedp.EvaluateAsDevTools(`$("#J-passCodeCoin").append("`+codeHTML+`")`,&useless),
chromedp.Click(`//a[@id="J-login"]`,chromedp.BySearch),
chromedp.WaitVisible(`//div[@class="center-welcome"]`,chromedp.BySearch),
)
if err!=nil{
cxtt=ctx
return cxtt,err
}else{
cxtt=ctx
return cxtt,nil
}
}else{
cxtt=ctx
return cxtt,errors.New("验证码处理错误")
}
}
func dataUrl2Img(dataurl string,file string,trimPrefix bool){
if trimPrefix{
dataurl=strings.Split(dataurl,",")[1]
}
imgBuf,err:=base64.StdEncoding.DecodeString(dataurl)
if err!=nil{
panic(err.Error())
}
ioutil.WriteFile(file,imgBuf,0666)
}
func analyseCapatcha(filePath string) string{
e_ctx,e_cancel:=chromedp.NewExecAllocator(context.Background(),chromedp.NoSandbox)
defer e_cancel()
ctx,cancel:=chromedp.NewContext(e_ctx,chromedp.WithErrorf(func (x string,ops ...interface{}){
fmt.Println("Error:-------------------------------->")
fmt.Printf(x,ops)
fmt.Println("\n<--------------------------------------")
}),chromedp.WithDebugf(func (x string,ops ...interface{}){
for _,v:=range ops{
str:=fmt.Sprintf(x,v)
if strings.Contains(str,"www.recaptcha.net")||strings.Contains(str,`recaptcha.google.cn`)||strings.Contains(str,`www.google.com/recaptcha`){
fmt.Println(str)
fmt.Println("Warning:This site may use google's Recaptcha to stop robot,to Crwal this site you try without Headless mode.")
}
}
}),chromedp.WithLogf(func (x string,ops ...interface{}){
fmt.Println("Log:-------------------------------->")
fmt.Printf(x,ops)
fmt.Println("\n<--------------------------------------")
}))
defer cancel()
var res string
err:=chromedp.Run(ctx,
chromedp.Navigate(cpatchaURL),
chromedp.WaitVisible("form",chromedp.BySearch),
chromedp.SetUploadFiles(`//form/input[@type='file']`,[]string{filePath},chromedp.BySearch),
chromedp.Click(`//form/input[@type="submit"]`),
chromedp.WaitVisible("//img",chromedp.BySearch),
chromedp.InnerHTML(`//p//font//b`,&res,chromedp.BySearch),
)
if err!=nil{
fmt.Println("请求站点错误")
panic(err.Error())
}
return res
}
func position2HTML(positionStr string) string{
positions:=strings.Split(positionStr," ")
res:=""
for _,v:=range positions{
p,_:=strconv.Atoi(v)
x,y:=35,41
if p>4{
y+=67
p-=4
}
x+=(p-1)*69
res+=`<div randcode='`+strconv.Itoa(x)+`,`+strconv.Itoa(y)+`' class='lgcode-active'></div>`
}
return res
}