使用selenium及打碼平台(自己還實作不了驗證碼圖片解析,對接打碼平台解析圖片驗證碼)現實自動登入亞馬遜網站,並修改帳號綁定信箱及密碼等操作。
邏輯:模擬瀏覽器的操作,定位元素填入數據,根據頁面元素取得屬性下載圖片驗證碼進行分析,自動填入驗證碼,實現自動化操作。
使用chrome瀏覽器,需要下載對應版本的webdriver 下載位址:。
#coding=utf-8from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitimport time ,sys, requests, jsonclass Browser(object):def __init__(self,url): self.browser = webdriver.Chrome() self.browser.maximize_window() self.url = url self.browser.get(self.url)def get_element_by_id(self,element_id): return self.browser.find_element_by_id(element_id)def get_element_by_class(self,_class):return self.browser.find_element_by_class_name(_class)def get_element_by_text(self,text):return self.browser.find_element_by_link_text(text)def get_screenshot(self,file):return self.browser.get_screenshot_as_file(file) def waitfor(self,element_id):return WebDriverWait(self.browser,10).until(lambda b:b.find_element_by_id(element_id).is_displayed())def upload_code_check(): url = ':5678/api.php?method=upload'print url data = {} data.update({'username':'xxxxx'}) data.update({'password':'xxxxx'}) data.update({'codetype':'5000'}) data.update({'appid':'1'}) data.update({'appkey':'22cc5376925e9387a23cf797cb9ba745'}) data.update({'timeout':'60'}) data.update({'version':'YAPI/WEB v1.0.0'}) data.update({'showimage':'1'}) files = {'file':('dd.jpg',open('C://dd.jpg','rb'),'image/jpeg')} res = requests.post(url,data=data,files=files) res_json = json.loads(res.content)print res.contentreturn res_json def get_code_check_results(url1):while True: res1 = requests.get(url1) res1 = json.loads(res1.content)if res1['ret'] == 0:breakreturn res1def get_image(img_url): img = requests.get(img_url) img_f = open('C://dd.jpg','w+b') img_f.write(img.content) img_f.close()return img def main(): url = ''count = 'xxxxxxx'pwd = 'xxxxxxxx'pwd_new = 'xxxxxxx'#email_new = 'xxxxx@qq.com'email_new = 'xxxxxxxx@qq.com'browser = Browser(url)if browser.waitfor('nav-link-yourAccount'): browser.get_element_by_id('nav-link-yourAccount').click()else:print 'connect timeout'sys.exit(1)if browser.waitfor('ap_email'): browser.get_element_by_id('ap_email').send_keys(count) browser.get_element_by_id('ap_password').send_keys(pwd) browser.get_element_by_id('signInSubmit').click()try: error = browser.get_element_by_id('auth-error-message-box')print 'login error'except Exception:passelse:print 'connect timeout'sys.exit(1)if browser.waitfor('nav-link-yourAccount'): browser.get_element_by_id('nav-link-yourAccount').click()else:print 'connect timeout'sys.exit(1)try: text = browser.get_element_by_text(u'登录和安全设置') text.click() email = browser.get_element_by_id('auth-cnep-edit-email-button') email.click()if browser.waitfor('ap_email_new'): browser.get_element_by_id('ap_email_new').send_keys(email_new) browser.get_element_by_id('ap_email_new_check').send_keys(email_new) browser.get_element_by_id('ap_password').send_keys(pwd) yanzhenma = browser.get_element_by_id('auth-captcha-image') img_url = yanzhenma.get_attribute('src') img_url = img_url.replace('&','&')print img_url img = get_image(img_url) res_json = upload_code_check() url1 = ":5678/api.php?method=result&cid=" + str(res_json['cid'])print url1 res1 = get_code_check_results(url1)print res1['text'] yzm = res1['text']print yzmif yzm: browser.get_element_by_id('auth-captcha-guess').send_keys(str(yzm)) browser.get_element_by_id('cnep_1B_submit_button').click()if browser.waitfor('auth-success-message-box'): browser.get_element_by_id('auth-cnep-edit-password-button').click()print 'success modify email'if browser.waitfor('ap_password_new_check'): browser.get_element_by_id('ap_password').send_keys(pwd) browser.get_element_by_id('ap_password_new').send_keys(pwd_new) browser.get_element_by_id('ap_password_new_check').send_keys(pwd_new) auth_img_url = browser.get_element_by_id('auth-captcha-image') img_url2 = auth_img_url.get_attribute('src').replace('&','&') img2 = get_image(img_url2) res_json = upload_code_check() url2 = ":5678/api.php?method=result&cid=" + str(res_json['cid'])print url2 res2 = get_code_check_results(url2)print res2['text'] yzm2 = res2['text']print yzm2if yzm2: browser.get_element_by_id('auth-captcha-guess').send_keys(str(yzm2)) browser.get_element_by_id('cnep_1D_submit_button').click() except NoSuchElementException,e:print(e) if __name__ == "__main__": main()
以上是selenium之實現自動登入的實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!