使用selenium及打碼平臺(自己還實現不了驗證碼圖片解析,對接打碼平臺解析圖片驗證碼)現實自動登錄亞馬遜網站,並修改賬號綁定郵箱及密碼等操作。 邏輯:模擬瀏覽器的操作,定位元素填入數據,根據頁面元素獲取屬性下載圖片驗證碼進行分析,自動填充驗證碼,實現自動化操作。 使用chrome瀏覽器,需要下載 ...
使用selenium及打碼平臺(自己還實現不了驗證碼圖片解析,對接打碼平臺解析圖片驗證碼)現實自動登錄亞馬遜網站,並修改賬號綁定郵箱及密碼等操作。
邏輯:模擬瀏覽器的操作,定位元素填入數據,根據頁面元素獲取屬性下載圖片驗證碼進行分析,自動填充驗證碼,實現自動化操作。
使用chrome瀏覽器,需要下載對應版本的webdriver 下載地址:https://chromedriver.storage.googleapis.com/index.html。
#coding=utf-8 from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait import time ,sys, requests, json class 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 = 'http://api.yundama.net: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.content return res_json def get_code_check_results(url1): while True: res1 = requests.get(url1) res1 = json.loads(res1.content) if res1['ret'] == 0: break return res1 def 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 = 'https://www.amazon.cn/' count = 'xxxxxxx' pwd = 'xxxxxxxx' pwd_new = 'xxxxxxx' #email_new = '[email protected]' email_new = '[email protected]' 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: pass else: 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 = "http://api.yundama.net: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 yzm if 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 = "http://api.yundama.net: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 yzm2 if 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()