Use selenium and the coding platform (you can’t parse the verification code image yourself, so you can connect to the coding platform to parse the image verification code) to automatically log in to the Amazon website, and modify the account binding email address and password.
Logic: Simulate the operation of the browser, fill in the data in the positioning element, download the image verification code based on the page element acquisition attribute for analysis, automatically fill in the verification code, and realize automated operations.
To use the chrome browser, you need to download the corresponding version of webdriver. Download address:.
#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()
The above is the detailed content of Selenium's example code for automatic login. For more information, please follow other related articles on the PHP Chinese website!