biliyan图片验证,1kkkvip图片抽正验证

###bili登录图片自动验证

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import time
from io import BytesIO
from PIL import Image
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

EMAIL = '394220162@qq.com'
PASSWORD = 'vff635241'
BORDER = 6
INIT_LEFT = 60


class CrackBilibili():
def __init__(self):
self.url = 'https://passport.bilibili.com/login'
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser, 20)
self.email = EMAIL
self.password = PASSWORD

def __del__(self):
self.browser.close()

def get_geetest_button(self):
"""
获取初始验证按钮
:return:
"""
button = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.gt_slider_knob.gt_show')))
return button

def get_position(self):
"""
获取验证码位置
:return: 验证码位置元组
"""
img = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.gt_cut_fullbg.gt_show')))
time.sleep(2)
location = img.location
size = img.size
top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[
'width']
return (top, bottom, left, right)

def get_screenshot(self):
"""
获取网页截图
:return: 截图对象
"""
screenshot = self.browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot

def get_slider(self):
"""
获取滑块
:return: 滑块对象
"""
slider = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.gt_slider_knob.gt_show')))
return slider

def get_geetest_image(self, name='captcha.png'):
"""
获取验证码图片
:return: 图片对象
"""
top, bottom, left, right = self.get_position()
print('验证码位置', top, bottom, left, right)
screenshot = self.get_screenshot()
captcha = screenshot.crop((left, top, right, bottom))
captcha.save(name)
return captcha

def open(self):
"""
打开网页输入用户名密码
:return: None
"""
self.browser.get(self.url)
# email = self.wait.until(EC.presence_of_element_located((By.ID, 'email')))
# password = self.wait.until(EC.presence_of_element_located((By.ID, 'password')))
# email.send_keys(self.email)
# password.send_keys(self.password)

def get_gap(self, image1, image2):
"""
获取缺口偏移量
:param image1: 不带缺口图片
:param image2: 带缺口图片
:return:
"""
left = 60
print('******大小*****')
print(image1.size)
for i in range(left, image1.size[0]):
for j in range(image1.size[1]):
if not self.is_pixel_equal(image1, image2, i, j):
left = i
return left
return left

def is_pixel_equal(self, image1, image2, x, y):
"""
判断两个像素是否相同
:param image1: 图片1
:param image2: 图片2
:param x: 位置x
:param y: 位置y
:return: 像素是否相同
"""
# 取两个图片的像素点
pixel1 = image1.load()[x, y]
pixel2 = image2.load()[x, y]
threshold = 60
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
pixel1[2] - pixel2[2]) < threshold:
return True
else:
return False

def get_track(self, distance):
"""
根据偏移量获取移动轨迹
:param distance: 偏移量
:return: 移动轨迹
"""
# 移动轨迹
track = []
# 当前位移
current = 0
# 减速阈值
mid = distance * 2/5
# 计算间隔
t = 0.2
# 初速度
v = 0

while current < distance:
if current < mid:
# 加速度为正2
a = 2
else:
# 加速度为负3
a = -3
# 初速度v0
v0 = v
# 当前速度v = v0 + at
v = v0 + a * t
# 移动距离x = v0t + 1/2 * a * t^2
move = v0 * t + 1 / 2 * a * t * t
# 当前位移
current += move
# 加入轨迹
track.append(round(move))
return track

def move_to_gap(self, slider, track):
"""
拖动滑块到缺口处
:param slider: 滑块
:param track: 轨迹
:return:
"""
ActionChains(self.browser).click_and_hold(slider).perform()
for x in track:
ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
time.sleep(0.5)
ActionChains(self.browser).release().perform()

def login(self):
"""
登录
:return: None
"""
submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'login-btn')))
submit.click()
time.sleep(10)
print('登录成功')

def crack(self):
# 输入用户名密码
self.open()
# 点击验证按钮
button = self.get_geetest_button()
print(button)
time.sleep(3)
ActionChains(self.browser).move_to_element(button).perform()
time.sleep(3)
# # 获取验证码图片(没有缺口的图)
image1 = self.get_geetest_image('captcha1.png')
# 点按呼出缺口
slider = self.get_slider()
slider.click()
time.sleep(4)
# 获取带缺口的验证码图片
image2 = self.get_geetest_image('captcha2.png')
# 获取缺口位置
gap = self.get_gap(image1, image2)
print('缺口位置', gap)
# 减去缺口位移
gap -= BORDER
# 获取移动轨迹
track = self.get_track(gap)
print('滑动轨迹', track)
# 拖动滑块
self.move_to_gap(slider, track)

# 验证结点里面是否包含此文字
success = self.wait.until(
EC.text_to_be_present_in_element((By.CLASS_NAME, 'gt_info_type'), '验证通过:'))
print(success)

# 失败后重试
# if not success:
# self.crack()
# else:
# self.login()


if __name__ == '__main__':
crack = CrackBilibili()
crack.crack()

###1kkk.vip抽正图片验证

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
from selenium import webdriver
# from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
# from urllib.parse import quote
# from lxml import etree
from PIL import Image
from io import BytesIO
import time
import random

import ikkk_get_img

chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.set_window_size(1400, 900)
# 显式等待 针对某个节点的等待
wait = WebDriverWait(browser, 10)


def get_times(item):
for k in range(0,4):
if ikkk_get_img.compare(item):
break
item = item.rotate(-90)
return k


def get_img():
screenshot = browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
screenshot.save('1kkk_full.png')
for i in range(1,5):
item = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, f'body > section.modal-wrap > div > div > div > div > div > div:nth-child({i+1})')))
loc = item.location
size = item.size
x1 = loc['x']
y1 = loc['y']
x2 = loc['x'] + size['width']
y2 = y1 + size['height']
item_file = screenshot.crop((x1, y1, x2, y2))
times = get_times(item_file)
print(times)
for i in range(times):
# time.sleep(1)
item.click()


def rellrnar():
username = '66980437@qq.com'
passwd = 'Zqwzqw199706'
input_username = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > section.modal-wrap > div > div > div > div > p:nth-child(2) > input[type="text"]')))
input_passwd = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > section.modal-wrap > div > div > div > div > p:nth-child(3) > input[type="password"]')))
input_username.send_keys(username)
input_passwd.send_keys(passwd)


def get_page():
url = 'http://www.1kkk.com/vipindex/'
browser.get(url)
login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > section.vip-banner-big.container-fluid > div > div > div.account-info > a')))
login_button.click()
time.sleep(0.2)
rellrnar()
get_img()
login = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#btnLogin')))
login.click()

def main():
get_page()


if __name__ == '__main__':
main()

###爬取1kkk验证图片并裁减去重

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
import requests
from PIL import Image
from io import BytesIO
import compare_helper
import os


def compare(item):
img_list = os.listdir('1kkk_img')
for temp in img_list:
temp = './1kkk_img/' + temp
if compare_helper.get_compare(item, temp) > 85:
return True
return False


def get_new(num):
url = 'http://www.1kkk.com/image3.ashx%3Ft=1546847130000'
res = requests.get(url)
screenshot = Image.open(BytesIO(res.content))
# screenshot.show()
for i in range(0, 4):
item = screenshot.crop((i * 76, 0, (i + 1) * 76, 76))
filename = f'1kkk_img/{num}_{i}.png'
flag = True
for k in range(0,4):
if compare(item):
flag = False
break
item = item.rotate(90)
if flag:
item.save(filename)
else:
print(f'{num}_{i}', '重复------------')


def get_num():
num = 0
img_list = os.listdir('1kkk_img')
for item in img_list:
num_temp = int(item.split('_')[0])
if num < num_temp:
num = num_temp
return num + 1


def main():
num = get_num()
while num:
get_new(num)
num += 1


if __name__ == '__main__':
main()

匹配图片

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
#!C:/Python27
#coding=utf-8

# from pytesser import *
from PIL import Image,ImageEnhance,ImageFilter
import os
import fnmatch
import re,time

import urllib, random


#import hashlib

def getGray(image_file):
tmpls=[]
for h in range(0, image_file.size[1]):#h
for w in range(0, image_file.size[0]):#w
tmpls.append( image_file.getpixel((w,h)) )

return tmpls

def getAvg(ls):#获取平均灰度值
return sum(ls)/len(ls)

def getMH(a,b):#比较100个字符有几个字符相同
dist = 0;
for i in range(0,len(a)):
if a[i]==b[i]:
dist=dist+1
return dist

def getImgHash_file(image_file):
# image_file = Image.open(fne) # 打开
image_file=image_file.resize((12, 12))#重置图片大小我12px X 12px
image_file=image_file.convert("L")#转256灰度图
Grayls=getGray(image_file)#灰度集合
avg=getAvg(Grayls)#灰度平均值
bitls=''#接收获取0或1
#除去变宽1px遍历像素
for h in range(1, image_file.size[1]-1):#h
for w in range(1, image_file.size[0]-1):#w
if image_file.getpixel((w,h))>=avg:#像素的值比较平均值 大于记为1 小于记为0
bitls=bitls+'1'
else:
bitls=bitls+'0'
return bitls

def getImgHash_path(fne):
image_file = Image.open(fne) # 打开
image_file=image_file.resize((12, 12))#重置图片大小我12px X 12px
image_file=image_file.convert("L")#转256灰度图
Grayls=getGray(image_file)#灰度集合
avg=getAvg(Grayls)#灰度平均值
bitls=''#接收获取0或1
#除去变宽1px遍历像素
for h in range(1, image_file.size[1]-1):#h
for w in range(1, image_file.size[0]-1):#w
if image_file.getpixel((w,h))>=avg:#像素的值比较平均值 大于记为1 小于记为0
bitls=bitls+'1'
else:
bitls=bitls+'0'
return bitls
'''
m2 = hashlib.md5()
m2.update(bitls)
print m2.hexdigest(),bitls
return m2.hexdigest()
'''

def get_compare(image_file1, filename2):
a=getImgHash_file(image_file1)#图片地址自行替换
b=getImgHash_path(filename2)
compare=getMH(a,b)
return compare

if __name__ == '__main__':
filename1 = './same/1537167265503_3.jpeg'
filename2 = './same/1537167265721_2.jpeg'
compare = get_compare(filename1, filename2)
print(compare)

###1kkkvip爬取第二种

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
import time
import os

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import quote
from lxml import etree
from PIL import Image
from io import BytesIO
import compare_helper
#
browser = webdriver.Chrome()
# 显式等待 针对某个节点的等待
wait = WebDriverWait(browser, 10)

screen_width = 1400
screen_height = 700
# browser = webdriver.Chrome()
browser.set_window_size(screen_width, screen_height)


# 取浏览器窗口内全图
def get_full_image():
# browser.execute_script('window.scrollTo(0, 300)')
screenshot = browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot


# 获取验证码小图
def get_captchas(full_image):
captcha_list = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.rotate-background')))
captchas = []
for captcha in captcha_list:
location = captcha.location
size = captcha.size
x1 = location['x']
y1 = location['y']
width = size['width']
height = size['height']
x2 = x1 + width
y2 = y1 + height

captcha = full_image.crop((x1 * 1.25, y1 * 1.25, x2 * 1.25, y2 * 1.25))
captcha.center_x = (x1 + width / 2) * 1.25
captcha.center_y = (y1 + width / 2) * 1.25
captchas.append(captcha)
captcha.save(f'./files/captchas/{time.time()}.png')
return captchas, captcha_list


# 计算验证码几次抽正
def cal_captcha(captcha, unique_list):
for unique in unique_list:
for i in range(4):
center_x, center_y = captcha.center_x, captcha.center_y
captcha = captcha.rotate(-90)
captcha.center_x, captcha.center_y = center_x, center_y
compare = compare_helper.get_compare2(captcha, unique)
if compare >= 80:
captcha.i = (i + 1) % 4
print(captcha.i, compare)
return captcha.i
print('没有库存')


def click_captcha(i, captcha):
time.sleep(1)
# ActionChains(browser).move_by_offset(xoffset=int(captcha.center_x), yoffset=int(captcha.center_y)).perform()
ActionChains(browser).move_to_element_with_offset(to_element=captcha, xoffset=10, yoffset=10).perform()
time.sleep(1)
for _ in range(i):
ActionChains(browser).click().perform()
print('进行了点击')
time.sleep(0.5)


def get_images(unique_list):

for i in range(50):
url = 'http://www.1kkk.com/vipindex/image3.ashx?t=1546847496000'
browser.get(url)
full_image = get_full_image()

img = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'img')))
location = img.location
size = img.size
x1 = location['x']
y1 = location['y']
width = size['width']
height = size['height']
x2 = x1 + width
y2 = y1 + height

big_img = full_image.crop((x1 * 1.25, y1 * 1.25, x2 * 1.25, y2 * 1.25))
big_img.save('./files/big_img' + str(i) + '.png')

# 裁剪小图
small_width = width / 4
small_height = height / 4
for num in range(4):
small_img = big_img.crop((num*small_width*1.25, 0, (num+1)*small_width*1.25, small_height*1.25))
img_name = f'small_img_{i}_{num}.png'
# small_img.img_name = img_name
if get_unique_img(small_img, unique_list):
unique_list.append(small_img)
small_img.save(f"./files/unique_imgs2/{img_name}")
return unique_list


# 小图去重
def get_unique_img(small_img, unique_list):

# img_names = os.listdir(r'D:\python_learning\spider\day05\files\small_imgs')
if not unique_list:
return True
for unique in unique_list:
for i in range(4):
# file_name = small_img.img_name
small_img = small_img.rotate(90)
# small_img.img_name = file_name
compare = compare_helper.get_compare2(small_img, unique)
# print(compare)
if compare >= 85:
print(compare)
return False
print(f'唯一的图+++++++++++++++++++++++++++++++++++++++++{small_img}')
return True


def get_page():

url = 'http://www.1kkk.com/vipindex/'
browser.get(url)
# 点击登录
# time.sleep(3)
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.header-avatar')))
button.click()


def put_unique_into_list():
img_file_names = os.listdir(r'D:\python_learning\spider\day05\files\unique_imgs2')
img_list = []
for img_file_name in img_file_names:
img_name = f"./files/unique_imgs2/{img_file_name}"
img = Image.open(img_name)
img.img_name = img_name
img_list.append(img)

return img_list


def main():
# 读取去重的小图
unique_list = put_unique_into_list()
# 获取去重的小图
# unique_list = get_images(unique_list)
get_page()

time.sleep(3)
full_image = get_full_image()
captchas, captcha_list = get_captchas(full_image)
for index in range(4):
i = cal_captcha(captchas[index], unique_list)
click_captcha(i, captcha_list[index])

# print(compare_helper.get_compare('./files/unique_imgs/small_img_1_3.png','./files/unique_imgs/small_img_11_2.png'))


if __name__ == '__main__':
main()

###匹配图片

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
#!C:/Python27
#coding=utf-8

# from pytesser import *
from PIL import Image,ImageEnhance,ImageFilter
import os
import fnmatch
import re,time

import urllib, random


#import hashlib

def getGray(image_file):
tmpls=[]
for h in range(0, image_file.size[1]):#h
for w in range(0, image_file.size[0]):#w
tmpls.append( image_file.getpixel((w, h)))

return tmpls


def getAvg(ls):#获取平均灰度值
return sum(ls)/len(ls)


def getMH(a,b):#比较100个字符有几个字符相同
dist = 0
for i in range(0,len(a)):
if a[i]==b[i]:
dist=dist+1
return dist


def getImgHash(fne):
image_file = Image.open(fne) # 打开
image_file=image_file.resize((12, 12))#重置图片大小我12px X 12px
image_file=image_file.convert("L")#转256灰度图
Grayls=getGray(image_file)#灰度集合
avg=getAvg(Grayls)#灰度平均值
bitls=''#接收获取0或1
#除去变宽1px遍历像素
for h in range(1, image_file.size[1]-1):#h
for w in range(1, image_file.size[0]-1):#w
if image_file.getpixel((w,h))>=avg:#像素的值比较平均值 大于记为1 小于记为0
bitls=bitls+'1'
else:
bitls=bitls+'0'
return bitls
'''
m2 = hashlib.md5()
m2.update(bitls)
print m2.hexdigest(),bitls
return m2.hexdigest()
'''


def get_compare(filename1, filename2):
a=getImgHash(filename1)#图片地址自行替换
b=getImgHash(filename2)
compare=getMH(a,b)
return compare


if __name__ == '__main__':
filename1 = './same/1537167265503_3.jpeg'
filename2 = './same/1537167265721_2.jpeg'
compare = get_compare(filename1, filename2)
print(compare)

###超级鹰验证码验证

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
#!/usr/bin/env python
# coding:utf-8

import requests
from hashlib import md5


class Chaojiying_Client(object):

def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}

def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()

def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()


def main1(urlstr):
# print(urlstr)
chaojiying = Chaojiying_Client('carmack', 'Vff635241', '96001')
im = open(urlstr, 'rb').read()
return chaojiying.PostPic(im, 1)['pic_str']


if __name__ == '__main__':
chaojiying = Chaojiying_Client('超级鹰账号', '密码', '96001') #用户中心>>软件ID 生成一个替换 96001
im = open('CaptchaImg.png', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 1902)) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()

###超级鹰验证

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
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import quote
from lxml import etree
from PIL import Image
from io import BytesIO
from chaojiying import main1
import time

chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)

# browser = webdriver.Chrome()
# 设置窗口大小
browser.set_window_size(1400, 700)
# 显式等待 针对某个节点的等待
wait = WebDriverWait(browser, 10)


def get_page():
url = 'http://bm.e21cn.com/log/reg.aspx'
browser.get(url)
html = browser.page_source
return html


# 取浏览器窗口内全图
def get_big_image():
browser.execute_script('window.scrollTo(0, 300)')
screenshot = browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot


# 取验证码坐标位置(左上角和右下角)
def get_position():
img = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#imgCheckCode')))
loc = img.location
size = img.size
print(loc)
print(size)
x1 = loc['x']
# 记住减去滚动高度
y1 = loc['y'] - 300
x2 = loc['x'] + size['width']
y2 = y1 + size['height']
return (x1, y1, x2, y2)


def parse_html(html):
# etree_html = etree.HTML(html)
screenshot = get_big_image()
screenshot.save('full_screen.png')

x1, y1, x2, y2 = get_position()
crop_image = screenshot.crop((x1, y1, x2, y2))
file_name = 'crop.png'
crop_image.save(file_name)
captha_str = main1(file_name)

username = '账号'
password = '密码'
tel = '电话'

print(captha_str)

input_username = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, 'input#username')))
input_password1 = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, 'input#pwd')))
input_password2 = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, 'input#pwd_Q')))
input_tel = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, 'input#tel')))
input_check = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, 'input#CheckCode')))
sublime = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input#btn_login')))
input_username.send_keys(username)
time.sleep(2)
input_password1.send_keys(password)
time.sleep(2)
input_password2.send_keys(password)
time.sleep(2)
input_tel.send_keys(tel)
time.sleep(2)
input_check.send_keys(captha_str)
time.sleep(2)
sublime.click()


def main():
html = get_page()
parse_html(html)


if __name__ == '__main__':
main()

###读取移动验证码

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
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from PIL import Image
from io import BytesIO
from chaojiying import main1

chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)

screen_width = 1400
screen_height = 700

browser.set_window_size(screen_width, screen_height)
# 显式等待 针对某个节点的等待
wait = WebDriverWait(browser, 5)

# 取浏览器窗口内全图
def get_big_image():
# browser.execute_script('window.scrollTo(0, 300)')
screenshot = browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
return screenshot

def get_captha_position():
captha = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, '#captchaImg')))
location = captha.location
size = captha.size
x1 = location['x']
y1 = location['y']
width = size['width']
height = size['height']
x2 = x1 + width
y2 = y1 + height
print(x1, y1, x2, y2)
print(width, height)
return (x1, y1, x2, y2)

def get_page():
url = 'https://login.10086.cn/html/login/login.html?channelID=12002&backUrl=https%3A%2F%2Fshop.10086.cn%2Fmall_280_280.html%3Fforcelogin%3D1'
browser.get(url)

button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#mail_login_2')))
button.click()

username = '账号'
password = '密码'
input_username = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, '#e_name')))
input_password = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, '#e_pwd')))

input_username.clear()
input_username.send_keys(username)
input_password.clear()
input_password.send_keys(password)
time.sleep(3)

full_screen_img = get_big_image()
print('--image size--')
print(full_screen_img.width, full_screen_img.height)

img_width = full_screen_img.width
img_height = full_screen_img.height

width_ratio = img_width / screen_width
print(width_ratio)

full_screen_img.save('mobile_login.png')

# 获取验证码左上角和右下角坐标
x1, y1, x2, y2 = get_captha_position()
x1, y1, x2, y2 = (x1 * width_ratio, y1, x2 * width_ratio, y2)

print('*' * 20)
print(x1, y1, x2, y2)

captha_img = full_screen_img.crop((x1, y1, x2, y2))
captha_img.save('mobile_captha.png')
captha_str = main1('mobile_captha.png')
print(captha_str)

input_code = wait.until(EC.presence_of_element_located
((By.CSS_SELECTOR, '#inputCode')))
input_code.clear()
input_code.send_keys(captha_str)

button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#submit_bt')))
button.click()


def main():
html = get_page()

if __name__ == '__main__':
main()