def get_code(driver, location): """ 返回验证码识别的内容 :param pic: :return: """ pic_1 = temporary + os.sep + str(time.time()) + ".png" # 1.先截图 driver.save_screenshot(pic_1) # 2.把验证码图抠出来 ce = driver.find_element(*location) left = ce.location['x'] top = ce.location['y'] right = ce.size['width'] + left height = ce.size['height'] + top # 3.保存抠的图 im = Image.open(pic_1) img = im.crop((left, top, right, height)) pic_2 = temporary + os.sep + str(time.time()) + '.png' img.save(pic_2) cjy = Chaojiying_Client('18301085980', 'star000', '909506') # 用户中心>>软件ID 生成一个替换 96001 im = open(pic_2, 'rb').read() return cjy.PostPic(im, 1902)['pic_str']
def change_pwd(self, brower, wait): brower.find_element_by_xpath('//a[text()="修改密码"]').click() wait.until( EC.presence_of_element_located( (By.XPATH, '//input[@name="oldpwd"]'))).send_keys(self.password) if self.password == 'salesmind1800': newpassword = '******' else: newpassword = '******' wait.until( EC.presence_of_element_located( (By.XPATH, '//input[@name="user_pwd"]'))).send_keys(newpassword) wait.until( EC.presence_of_element_located( (By.XPATH, '//input[@data-nick="ok_new_password"]' ))).send_keys(newpassword) while True: imgelement = wait.until( EC.presence_of_element_located( (By.XPATH, '//img[@data-selector="ver-code"]'))) location = imgelement.location # 获取滑条x,y轴坐标 if location['x'] == 0: return brower.save_screenshot(BASE_DIR + '/' + 'liepin_.png') # 截取当前网页,该网页有我们需要的验证码 size = imgelement.size # 获取滑条的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open(BASE_DIR + '/' + "liepin_.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save(BASE_DIR + '/' + 'liepinyzm.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '1902') im = open(BASE_DIR + '/' + 'liepinyzm.png', 'rb').read() res = chaojiying.PostPic(im, 1902) pic_str = res['pic_str'] os.remove(BASE_DIR + '/' + "liepin_.png") os.remove(BASE_DIR + '/' + "liepinyzm.png") inpt = brower.find_element_by_xpath('//input[@name="verifycode"]') inpt.clear() time.sleep(1) inpt.send_keys(pic_str) time.sleep(1) brower.find_element_by_xpath('//button[@type="submit"]').click() time.sleep(1) try: button = brower.find_element_by_xpath('//a[@data-name="ok"]') if button: chaojiying.ReportError(res['pic_id']) button.click() time.sleep(2) imgelement.click() except: break time.sleep(1) return newpassword
def login(self): self.driver.get('https://ehire.51job.com/') js1 = "document.getElementsByClassName('headerBanner')[0].style.display='none'" js2 = "document.getElementsByClassName('topHeader')[0].style.display='none'" self.driver.execute_script(js1) self.driver.execute_script(js2) self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="txtMemberNameCN"]'))).send_keys( self.vipname) self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="txtUserNameCN"]'))).send_keys(self.username) self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="txtPasswordCN"]'))).send_keys(self.password) self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="btnBeginValidate"]'))).click() while True: time.sleep(1) imgelement = self.driver.find_element_by_xpath('//*[@id="divValidateHtml"]/div') # 定位验证码 self.driver.save_screenshot('aa.png') # 截取当前网页,该网页有我们需要的验证码 location = imgelement.location # 获取验证码x,y轴坐标 print(location) if location['x'] == 0: return size = imgelement.size # 获取验证码的长宽 print(size['width']) rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open("aa.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('frame4.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open('frame4.png', 'rb').read() while True: try: res = chaojiying.PostPic(im, 9004) except Exception as E: print(E) time.sleep(2) continue break content = res['pic_str'].split('|') loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(self.driver) for loca in loc_list: action.move_to_element_with_offset(imgelement, int(loca[0]), int(loca[1])).click() action.pause(1) action.perform() time.sleep(1) self.wait.until(EC.presence_of_element_located((By.LINK_TEXT, '验 证'))).click() time.sleep(1) success = self.driver.find_element_by_xpath('//*[@id="btnEndValidate"]').text if success == '验证通过': break else: chaojiying.ReportError(res['pic_id']) time.sleep(3) self.driver.find_element_by_xpath('//*[@id="Login_btnLoginCN"]').click()
def __init__(self): """ 初始化 """ self.url = URL self.browser = webdriver.Chrome() self.wait = WebDriverWait(self.browser, TIME_OUT) # 简书登录账号、密码 self.email = EMAIL self.password = PASSWORD # 创建超级鹰Client对象 self.chaojiying = Chaojiying_Client(CHAIJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAIJIYING_SOFT_ID)
def yzm1(self): while True: # 定位滑条 imgelement1 = self.wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="nc_1__scale_text"]'))) self.driver.save_screenshot('qichacha.png') # 截取当前网页,该网页有我们需要的验证码 location = imgelement1.location # 获取滑条x,y轴坐标 size1 = imgelement1.size # 获取滑条的长宽 # 定位验证码 try: imgelement2 = self.driver.find_element_by_xpath( '//*[@id="nc_1_clickCaptcha"]') except: return size2 = imgelement2.size # 获取验证码的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size1['width']), int(location['y'] + size1['height'] + size2['height']) ) # 写成我们需要截取的位置坐标 i = Image.open("qichacha.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('qichachayzm.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9101') im = open('qichachayzm.png', 'rb').read() res = chaojiying.PostPic(im, 9101) content = res['pic_str'] loca = content.split(',') action = ActionChains(self.driver) action.move_to_element_with_offset(imgelement1, int(loca[0]), int(loca[1])) action.click() action.perform() time.sleep(1) try: text = self.driver.find_element_by_xpath( '//*[@id="nc_1__scale_text"]/span/b').text if text == '验证通过': break except Exception: # print('验证失败') chaojiying.ReportError(res['pic_id']) time.sleep(1) try: self.driver.find_element_by_xpath('//*[@id="verify"]').click() time.sleep(1) except: pass
def yzm2(self): while True: imgelement = self.wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="nc_1__imgCaptcha_img"]/img'))) location = imgelement.location # 获取滑条x,y轴坐标 if location['x'] == 0: return self.driver.save_screenshot('qichacha.png') # 截取当前网页,该网页有我们需要的验证码 size = imgelement.size # 获取滑条的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open("qichacha.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('qichachayzm.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '1902') im = open('qichachayzm.png', 'rb').read() res = chaojiying.PostPic(im, 1902) pic_str = res['pic_str'] os.remove("qichacha.png") os.remove("qichachayzm.png") inpt = self.driver.find_element_by_xpath( '//*[@id="nc_1_captcha_input"]') inpt.clear() time.sleep(1) inpt.send_keys(pic_str) time.sleep(1) self.driver.find_element_by_xpath( '//*[@id="nc_1_scale_submit"]/span').click() time.sleep(1) try: txt = self.driver.find_element_by_xpath( '//*[@id="nc_1__captcha_img_text"]/span').text if txt: chaojiying.ReportError(res['pic_id']) time.sleep(2) except: break time.sleep(1) try: self.driver.find_element_by_xpath('//*[@id="verify"]').click() time.sleep(1) except: pass
from lxml import etree import base64 from utils import rsa from utils.chaojiying import Chaojiying_Client import private from base import Request import re chaojiying = Chaojiying_Client(private.CODE_USERNAME, private.CODE_PASSWORD, private.CODE_NUMBER) class YiLogin(Request): ''' 直接通过模拟易班登录方式进行登录 ''' def __init__(self, username, password): self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36', } self.headers2 = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest', } self.username = username self.password = password def login(self): cookies, public_key = self.access() # 进行加密 password = self._encrypt(self.password, public_key)
class Crack_Jianshu(object): def __init__(self): """ 初始化 """ self.url = URL self.browser = webdriver.Chrome() self.wait = WebDriverWait(self.browser, TIME_OUT) # 简书登录账号、密码 self.email = EMAIL self.password = PASSWORD # 创建超级鹰Client对象 self.chaojiying = Chaojiying_Client(CHAIJIYING_USERNAME, CHAOJIYING_PASSWORD, CHAIJIYING_SOFT_ID) def __del__(self): """ gc机制关闭浏览器 """ self.browser.close() def open(self): """ 打开简书网页版登录界面输入邮箱账号、密码 :return: None """ self.browser.get(self.url) # 邮箱 email = self.wait.until(EC.presence_of_element_located((By.ID, 'session_email_or_mobile_number'))) # 密码 password = self.wait.until(EC.presence_of_element_located((By.ID, 'session_password'))) # 输入邮箱 email.clear() email.send_keys(self.email) sleep(2) # 输入密码 password.clear() password.send_keys(self.password) sleep(2) def get_submit_btn(self): """ 获取登录按钮 :return: button """ button = self.wait.until(EC.element_to_be_clickable((By.ID, 'sign-in-form-submit-btn'))) return button def get_touclick_element(self): """ 获取验证码图片对象 :return: 图片对象 """ element = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_item_img'))) return element def get_code_position(self): """ 获取验证码位置 :return: 验证码位置列表 """ element = self.get_touclick_element() sleep(3) # 相对位置 location = element.location # 宽高度 size = element.size # 坐标值 top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size['width'] # 验证码左上角和右下角坐标 return [left, top, right, bottom] def get_screenshot(self): """ 获取网页截图 :return: 截图对象 """ screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_touclick_image(self, name='captcha.png'): """ 获取验证码图片 :return: 图片对象 """ left, top, right, bottom = self.get_code_position() print('验证码位置:', left, top, right, bottom) # 获取网页截图的Image对象 screenshot = self.get_screenshot() # 获取验证码的Image对象 jianshu_code = screenshot.crop((left, top, right, bottom)) # 存储 jianshu_code.save(name) return jianshu_code def get_points(self, captcha_result): """ 解析超级鹰识别结果 :param captcha_result: 识别结果 :return: 转化结果 """ # 获取pic_str的values groups = captcha_result.get('pic_str').split('|') # 将 字符串坐标值 转换为 整数型的坐标值 locations = [[int(number) for number in group.split(',')] for group in groups] return locations def touch_click_words(self, locations): """ 点击验证图片 :param locations: 点击位置 :return: None """ cnt = 1 for location in locations: print('坐标点{}: {}'.format(cnt,location)) ActionChains(self.browser).move_to_element_with_offset(self.get_touclick_element(),location[0], location[1]).click().perform() cnt = cnt + 1 sleep(1) def get_verifi_button(self): """ 确认按钮 :return: None """ submit = self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@class="geetest_commit_tip"]'))) submit.click() def get_article_info(self): pass def connect_db(self): pass def save_to_db(self): pass def crack_login(self): """ 登录 :return: None """ # 打开简书登录界面 self.open() # 点击登录按钮 button = self.get_submit_btn() button.click() # 获取验证码图片 image = self.get_touclick_image() bytes_array = BytesIO() # 存储为字节流格式 image.save(bytes_array, format='PNG') # 识别验证码 result = self.chaojiying.PostPic(bytes_array.getvalue(), CHAOJIYING_KIND) print("\n超级鹰识别结果:{}\n".format(result)) locations = self.get_points(result) self.touch_click_words(locations) sleep(3) # 点击确认按钮 self.get_verifi_button() # 通过获取"Logo"判断是否登录成功 sleep(5) success = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'logo'))) if success: print('\nSuccessful login!\n') # 失败重试 if not success: print("-" * 50) self.crack_login()
def check_yzm(self): """ 验证码处理: 1·捕获验证码,对当前页面进行截屏 2·定位验证码图片位置,并从截屏中提取 3·调用打码平台,破解验证码,返回验证码参数 4·对验证码参数进一步处理,并模拟人工操作破解验证码 5·验证是否破解成功,否则发送验证失败api :return: """ while True: try: imgelement = self.driver.find_element_by_xpath('//*[@id="divValidateHtml"]/div') # 定位验证码 self.driver.save_screenshot('aa.png') # 截取当前网页,该网页有我们需要的验证码 location = imgelement.location # 获取验证码x,y轴坐标 if location['x'] == 0: return size = imgelement.size # 获取验证码的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open("aa.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('frame4.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open('frame4.png', 'rb').read() while True: try: res = chaojiying.PostPic(im, 9004) except Exception as E: print(E) time.sleep(2) continue break content = res['pic_str'].split('|') print(res) loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(self.driver) for loca in loc_list: action.move_to_element_with_offset(imgelement, int(loca[0]), int(loca[1])).click() action.pause(1) action.perform() time.sleep(1) self.wait.until(EC.presence_of_element_located((By.LINK_TEXT, '验 证'))).click() time.sleep(1) self.driver.refresh() imgelement = self.driver.find_element_by_xpath('//*[@id="divValidateHtml"]/div') # 定位验证码 print(imgelement, '111111') self.driver.save_screenshot('d://aa.png') # 截取当前网页,该网页有我们需要的验证码 location = imgelement.location # 获取验证码x,y轴坐标 print(location) if location['x'] == 0: print('验证成功') else: print('验证失败') chaojiying.ReportError(res['pic_id']) time.sleep(3) except: print('没有出现验证码') break
def login(self): chrome_options = Options() chrome_options.add_argument('window-size=1920x2000') # 指定浏览器分辨率 chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面 chrome_options.add_argument( '--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 # brower = webdriver.Chrome(chrome_options=chrome_options) brower = webdriver.Chrome() brower.maximize_window() wait = WebDriverWait(brower, 10) brower.get('https://passport.liepin.com/e/account') url1 = brower.current_url while True: yz_error = False newpassword = None if self.sign == 0: wait.until( EC.presence_of_element_located(( By.XPATH, '//*[@id="user-reglpt"]/div[1]/div[2]/div/div[1]/ul/li[2]' ))).click() user = wait.until( EC.presence_of_element_located(( By.XPATH, '//section[@data-selector="PC"]//input[@name="user_login"]' ))) user.clear() for item in self.username: user.send_keys(item) time.sleep(random.uniform(0.3, 0.6)) time.sleep(1) pwd = wait.until( EC.presence_of_element_located(( By.XPATH, '//section[@data-selector="PC"]//input[@name="user_pwd"]' ))) pwd.clear() for item in self.password: pwd.send_keys(item) time.sleep(random.uniform(0.3, 0.6)) time.sleep(1) wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="verify-button"]'))).click() while True: if self.sign == 0: imgelement = brower.find_element_by_xpath( '//*[@id="captcha"]/iframe') # 定位验证码 location = imgelement.location # 获取验证码x,y轴坐标 if location['x'] == 0: return brower.save_screenshot( 'liepin.png') # 截取当前网页,该网页有我们需要的验证码 size = imgelement.size # 获取验证码的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']) ) # 写成我们需要截取的位置坐标 i = Image.open('liepin.png') # 打开截图 frame = i.crop( rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame.save('liepinframe.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open('liepinframe.png', 'rb').read() while True: try: res = chaojiying.PostPic(im, 9004) except Exception as E: time.sleep(2) continue break os.remove('liepin.png') os.remove('liepinframe.png') content = res['pic_str'].split('|') loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(brower) for loca in loc_list: action.move_to_element_with_offset( imgelement, int(loca[0]), int(loca[1])).click() action.move_to_element_with_offset( imgelement, 210, 245).click() action.perform() time.sleep(5) url2 = brower.current_url for i in range(2): if url1 != url2: try: node = brower.find_element_by_xpath( '//h1[text()="账号登录异常"]') if node: newpassword = self.change_pwd( brower, wait) brower.find_element_by_xpath( '//*[text()="立即登录"]').click() break except: break else: time.sleep(3) else: yz_error = True break else: break if yz_error: continue if newpassword: self.password = newpassword task = SpiderTask.objects.get_one_task(id=self.task_id) data = json.loads(task.param) data["password"] = newpassword task.param = json.dumps(data) task.save() continue cookie = [ item["name"] + "=" + item["value"] for item in brower.get_cookies() ] cookiestr = '; '.join(item for item in cookie) print(cookiestr) self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36', 'Cookie': cookiestr, 'X-Requested-With': 'XMLHttpRequest', 'Referer': 'https://lpt.liepin.com/cvsearch/showcondition/', 'Host': 'lpt.liepin.com', 'Origin': 'https://lpt.liepin.com' } brower.quit() break else: break
def yzm(self): """ 浏览器模拟登陆处理验证码 :return: """ driver = self.login() driver.get('https://antirobot.tianyancha.com/captcha/verify?') time.sleep(2) while True: try: imgelement = self.driver.find_element_by_xpath( '//div[@class="new-box94"]') # 定位验证码 self.driver.save_screenshot( 'tianyancha.png') # 截取当前网页,该网页有我们需要的验证码 location = imgelement.location # 获取验证码x,y轴坐标 if location['x'] == 0: return size = imgelement.size # 获取验证码的长宽 # print(size['width']) rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open("tianyancha.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('frame_tyc.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open('frame_tyc.png', 'rb').read() while True: try: res = chaojiying.PostPic(im, 9004) except Exception as E: # print(E) continue break content = res['pic_str'].split('|') loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(self.driver) for loca in loc_list: action.move_to_element_with_offset(imgelement, int(loca[0]), int(loca[1])).click() action.perform() time.sleep(1) self.wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="submitie"]'))).click() time.sleep(1) self.driver.refresh() imgelement = self.driver.find_element_by_xpath( '//div[@class="new-box94"]') # 定位验证码 location = imgelement.location # 获取验证码x,y轴坐标 if location['x'] == 0: # print('验证成功') break else: # print('验证失败') chaojiying.ReportError(res['pic_id']) except Exception: break driver.quit()
def login(self): """ 登录网站 :return: """ chrome_options = Options() chrome_options.add_argument('window-size=1920x1500') # 指定浏览器分辨率 chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面 chrome_options.add_argument( '--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 # driver = webdriver.Chrome() driver = webdriver.Chrome(chrome_options=chrome_options) driver.maximize_window() wait = WebDriverWait(driver, 5) driver.get('https://ehire.51job.com/') time.sleep(1) while True: wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="txtMemberNameCN"]'))).send_keys(self.vipname) wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="txtUserNameCN"]'))).send_keys(self.username) wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="txtPasswordCN"]'))).send_keys(self.password) wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="btnBeginValidate"]'))).click() time.sleep(1) while True: imgelement = driver.find_element_by_xpath( '//*[@id="divValidateHtml"]/div') # 定位验证码 location = imgelement.location # 获取验证码x,y轴坐标 if location['x'] == 0: return driver.save_screenshot(BASE_DIR + '/' + self.table_name + '_job.png') # 截取当前网页,该网页有我们需要的验证码 size = imgelement.size # 获取验证码的长宽 rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标 i = Image.open(BASE_DIR + '/' + self.table_name + '_job.png') # 打开截图 frame = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame.save(BASE_DIR + '/' + self.table_name + 'frame.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open(BASE_DIR + '/' + self.table_name + 'frame.png', 'rb').read() while True: try: res = chaojiying.PostPic(im, 9004) except Exception as E: time.sleep(2) continue break os.remove(BASE_DIR + '/' + self.table_name + '_job.png') os.remove(BASE_DIR + '/' + self.table_name + 'frame.png') content = res['pic_str'].split('|') loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(driver) for loca in loc_list: action.move_to_element_with_offset(imgelement, int(loca[0]), int(loca[1])).click() action.perform() time.sleep(1) wait.until( EC.presence_of_element_located( (By.LINK_TEXT, '验 证'))).click() time.sleep(1) success = driver.find_element_by_xpath( '//*[@id="btnEndValidate"]').text if success == '验证通过': break else: chaojiying.ReportError(res['pic_id']) time.sleep(3) driver.find_element_by_xpath('//*[@id="Login_btnLoginCN"]').click() try: wait.until( EC.presence_of_element_located( (By.XPATH, '//a[text()="强制下线"]'))).click() time.sleep(2) url = driver.current_url if 'Navigate.aspx?' in url: break else: continue except: break time.sleep(5) cookie = [ item["name"] + "=" + item["value"] for item in driver.get_cookies() ] cookiestr = '; '.join(item for item in cookie) print(cookiestr) driver.quit() return cookiestr
def login(self): """ 登录网站 :return: """ options = webdriver.FirefoxOptions() options.add_argument('-headless') options.add_argument('--disable-gpu') driver = webdriver.Firefox(firefox_options=options) # driver = webdriver.Firefox() driver.maximize_window() wait = WebDriverWait(driver, 5) driver.get('https://passport.zhaopin.com/org/login') time.sleep(2) wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="loginName"]'))).send_keys(self.username) time.sleep(2) wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="password"]'))).send_keys(self.password) time.sleep(2) while True: wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="loginbutton"]'))).click() time.sleep(2) imgelement = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_head"]'))) # 定位验证码 location1 = imgelement.location # 获取验证码x,y轴坐标 if location1['x'] == 0: continue else: break while True: imgelement1 = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_head"]'))) # 定位验证码 imgelement2 = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_table_box"]'))) # 定位验证码 location1 = imgelement1.location # 获取验证码x,y轴坐标 if location1['x'] == 0: break driver.save_screenshot('aa.png') # 截取当前网页,该网页有我们需要的验证码 size1 = imgelement1.size # 获取验证码的长宽 size2 = imgelement2.size # 获取验证码的长宽 rangle = (int(location1['x']), int(location1['y']), int(location1['x'] + size1['width']), int(location1['y'] + size1['height'] + size2['height']) ) # 写成我们需要截取的位置坐标 i = Image.open("aa.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('frame4.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open('frame4.png', 'rb').read() res = chaojiying.PostPic(im, 9004) content = res['pic_str'].split('|') print(res) loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(driver) loc1 = loc_list[0] try: x = int(loc1[0]) except Exception as E: print(E) chaojiying.ReportError(res['pic_id']) continue for loca in loc_list: action.move_to_element_with_offset(imgelement1, int(loca[0]), int(loca[1])).click() action.pause(random.uniform(1, 2)) action.perform() time.sleep(1) wait.until( EC.presence_of_element_located( (By.XPATH, '//a[@class="geetest_commit"]'))).click() time.sleep(2) try: imgelement = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_widget"]'))) location = imgelement.location if location['x'] == 0: break else: chaojiying.ReportError(res['pic_id']) time.sleep(1) continue except: break try: input_phone = driver.find_element_by_xpath('//*[@id="vmobile"]') pc = PhoneCode() phone = pc.get_phone() input_phone.send_keys(phone) driver.find_element_by_xpath( '//a[@class="verify_code_btn"]').click() while True: imgelement1 = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_head"]'))) # 定位验证码 imgelement2 = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_table_box"]'))) # 定位验证码 driver.save_screenshot('aa.png') # 截取当前网页,该网页有我们需要的验证码 location1 = imgelement1.location # 获取验证码x,y轴坐标 print(location1) if location1['x'] == 0: break size1 = imgelement1.size # 获取验证码的长宽 size2 = imgelement2.size # 获取验证码的长宽 print(size1['width']) rangle = (int(location1['x']), int(location1['y']), int(location1['x'] + size1['width']), int(location1['y'] + size1['height'] + size2['height'])) # 写成我们需要截取的位置坐标 i = Image.open("aa.png") # 打开截图 frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域 frame4.save('frame4.png') chaojiying = Chaojiying_Client('jackie1102', 'salesmind1800', '9004') im = open('frame4.png', 'rb').read() res = chaojiying.PostPic(im, 9004) content = res['pic_str'].split('|') print(res) loc_list = [] for i in content: loc = i.split(',') loc_list.append(loc) action = ActionChains(driver) loc1 = loc_list[0] try: x = int(loc1[0]) except Exception as E: print(E) chaojiying.ReportError(res['pic_id']) continue for loca in loc_list: action.move_to_element_with_offset(imgelement1, int(loca[0]), int(loca[1])).click() action.pause(random.uniform(1, 2)) action.perform() time.sleep(1) wait.until( EC.presence_of_element_located( (By.XPATH, '//a[@class="geetest_commit"]'))).click() time.sleep(2) try: imgelement = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="geetest_widget"]'))) location = imgelement.location if location['x'] == 0: break else: chaojiying.ReportError(res['pic_id']) time.sleep(1) continue except: break code = pc.get_code(phone) input_code = driver.find_element_by_xpath('//*[@id="verifyCode"]') input_code.send_keys(code) driver.find_element_by_xpath('//*[@id="confirm_btn"]').click() except: pass time.sleep(5) cookie = [ item["name"] + "=" + item["value"] for item in driver.get_cookies() ] cookiestr = '; '.join(item for item in cookie) print(cookiestr) driver.quit() return cookiestr