前言 本文是使用python模拟登录网站的两次练习总结。仅使用账号密码登录,我之后会尝试使用cookie登录。
程序 登录4399 柿子要挑软的捏,所以我挑选了我的童年4399。 它没有可恶的人机验证,我原本想登录成功后在发出请求完成签到呢,结果登录成功就给我自动签到了。 按F12打开控制台找到账号密码输入框、登录按钮,仅此而已,不愧是我的童年。
代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome() driver.get('https://u.4399.com/login.html?refer=%2Fprofile%2F' ) driver.switch_to.frame('embed_login_frame' ) username_input = WebDriverWait(driver, 10 ).until( EC.presence_of_element_located((By.ID, 'username' )) ) username_input = driver.find_element(By.ID, 'username' ) password_input = driver.find_element(By.ID, 'j-password' ) username_input.send_keys('aaa' ) password_input.send_keys('aaa' ) submit_button = driver.find_element(By.CSS_SELECTOR, '.ptlogin_btn' ) submit_button.click() current_url = driver.current_url if current_url == 'https://u.4399.com/profile/' : print ('登录成功' ) else : print ('登录失败' ) while (1 ): { }
登录百度贴吧 模拟登录时,会遇到旋转图片验证,这我就不会了,但github上有一个大佬破解了这个,我之后会尝试的。 我在检查登录页面时发现了从不同位置登录,账号密码的id是不同的,不知道为什么。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 from selenium import webdriverimport requestsfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Edge() driver.get('https://tieba.baidu.com/' ) login_link = driver.find_element(By.CSS_SELECTOR, 'a[class="btn_login"]' ) login_link.click() switch_to_password_login_link = WebDriverWait(driver, 10 ).until( EC.element_to_be_clickable((By.ID, 'TANGRAM__PSP_4__footerULoginBtn' )) ) switch_to_password_login_link.click() username_input = driver.find_element(By.ID, 'TANGRAM__PSP_4__userName' ) username_input.send_keys('qwer1234' ) password_input = driver.find_element(By.ID, 'TANGRAM__PSP_4__password' ) password_input.send_keys('Qwer1234' ) login_button = driver.find_element(By.ID, 'TANGRAM__PSP_4__submit' ) login_button.click() cookies = driver.get_cookies() cookies_dict = {cookie['name' ]: cookie['value' ] for cookie in cookies} headers = { 'Referer' : 'https://tieba.baidu.com/sign/add' , } params = { 'ie' : 'utf-8' , 'kw' : '孙笑川' , } response = requests.post( 'https://tieba.baidu.com/sign/add' , headers=headers, params=params, cookies=cookies_dict ) if response.status_code == 200 : print ('签到成功!' ) else : print (f'签到失败,状态码:{response.status_code} ' ) while (1 ): print (1 )