python登录京东滑动验证_selenium模拟登录京东滑块验证码from selenium importwebdriverfrom selenium.webdriver.support.ui importWebDriverWaitfrom
selenium.webdrivermon.action_chains importActionChainsfrom PIL
importImageimporttimeimportbase64importosimportioimportrandomclassJingdong():def __init__(self, username, password): self.username=username
self.password=password
self.url= 'passport.jd/new/login.aspx?'self.options=webdriver.ChromeOptions()springcloud思维导图
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.browser= webdriver.Chrome(options=self.options, executable_path='D:\Program Files (x86)\')
self.wait= WebDriverWait(self.browser, 10)
self.browser.maximize_window()defopen_browser(self):
(self.url)
time.sleep(2)
self.browser.find_element_by_class_name('login-tab-r').click()
time.sleep(1)源代码男主问女主
self.browser.find_element_by_id('loginname').send_keys(self.username)
self.browser.find_element_by_id('nloginpwd').send_keys(self.password)
self.browser.find_element_by_class_name('login-btn').click()
time.sleep(1)defget_img_url(self):#获取验证码缺⼝图⽚的地址
eclipse创建java程序img_url = self.browser.find_element_by_class_name('JDJRV-
bigimg').find_element_by_css_selector('img').get_attribute('src')returnimg_url
@staticmethoddefget_image(image1):#将验证码的缺⼝图⽚与已有⽂件夹中的完整图⽚进⾏⽐较以取得其对应的完整图⽚
difference =0
image_=image1
size=image1.size
images= os.listdir('./img2')
pixel1=image1.load()for image inimages:
image= Image.open(f'./img2/{image}')
pixel2=image.load()
diff=0for j in range(size[1]):for i inrange(size[0]):
delta= abs(pixel1[i, j][0] - pixel2[i, j][0]) + abs(pixel1[i, j][1] - pixel2[i, j][1]) +abs(
pixel1[i, j][2] - pixel2[i, j][2])
diff+=deltaif difference ==0:
difference=diff
image_=imageelif difference >diff:
difference=diff
image_=imagereturnimage_
@staticmethoddefis_pixel_equal(image1, image2, x, y):#判断两张图⽚相同坐标的rgb数值是否近似
pixel1 =image1.load()[x, y]
pixel2=image2.load()[x, y]
threshold= 5
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold andabs(
pixel1[2] - pixel2[2])
left = 60img__img_url()
image1_bytes= base64.urlsafe_b64decode(img_url.split(',')[-1])
image1= Image.open(io.BytesIO(image1_bytes)).convert("RGB")
_image(image1)if notimage2:
京东python入门教程self.browser.find_element_by_class_name('JDJRV-img-refresh').click()
time.sleep(1)
<_gap()else:for i inrange(left, image1.size[0]):for j in range(image1.size[1]):if notself.is_pixel_equal(image1, image2, i, j):returnireturnleft
@staticmethoddefget_track(distance):#轨迹⽣成
track =[]
track_=[]
delta_t= 0.1
for i in range(1, 21):
delta_dis= 1 / 12 * distance * (delta_t * i) ** 3 - 1 / 12 * distance * (delta_t * (i - 1)) ** 3track.append(round(delta_dis))for i in range(1, 10):
delta_dis= 1 / 3 * distance * (i * delta_t) ** 3 - 1 / 3 * distance * ((i - 1) * delta_t) ** 3track_.append(round(delta_dis))wordvba
track.append(0)while len(track_) >0:
track.append(track_.pop())
track[20] = distance -sum(track)returntrackdefverify(self):#滑动滑块进⾏验证
distince =_gap()if distince > 180:
distince= distince - abs(180-distince)/180*40 - 40
java锁的类型有哪些else:
distince= distince + abs(180-distince)/180*40 - _track(distince)
slider= self.browser.find_element_by_class_name('JDJRV-slide-btn')
ActionChains(self.browser).click_and_hold(slider).perform()for axis intracks:
yoffset= random.randint(-2, 2)
ActionChains(self.browser).move_by_offset(xoffset=axis, yoffset=yoffset).perform()
time.sleep(0.5)
ActionChains(self.browser).release().perform()
time.sleep(3)if self.browser.current_url !=self.url:print("登录成功") time.sleep(3)
self.browser.close()else:
self.verify()defmain(self):
self.open_browser()
time.sleep(1)
self.verify()if __name__ == '__main__':
jingdong= Jingdong('username', 'password')
jingdong.main()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论