python selenium 瀏覽器登入並獲取cookie
阿新 • • 發佈:2021-08-11
一、各模組說明
selenium.webdriver 瀏覽器驅動 selenium.webdriver.common.by 選擇器型別 selenium.webdriver.support.wait.WebDriverWait 等待控制 selenium.webdriver.support.expected_conditions 條件控制 selenium.common.exceptions 異常型別
二、匯入相關模組
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException
三、載入驅動
# 按驅動型別載入驅動 if Path('./chromedriver.exe').exists(): driver = webdriver.Chrome() elif Path('./geckodriver.exe').exists(): driver = webdriver.Firefox() else: print('異常:未找到瀏覽器驅動') print('提示:請下載對應版本的瀏覽器驅動,並放置於目錄:' + os.getcwd()) print('chrome: http://npm.taobao.org/mirrors/chromedriver/') print('Firefox: http://npm.taobao.org/mirrors/geckodriver/') exit(0)
四、開啟網頁
# 視窗最大化 driver.maximize_window() # 開啟網頁 driver.get('https://xxxxx')
五、元素獲取及點選
driver.find_element(By.CSS_SELECTOR, '.btn-login').click()
注:這裡用的是 css 選擇器,其他選擇器參考這裡:定位元素 | Selenium
六、輸入賬號密碼並提交登入
driver.find_element(By.CSS_SELECTOR, '.phonenum_input').send_keys(phone) driver.find_element(By.CSS_SELECTOR, '.password_input').send_keys(password) driver.find_element(By.CSS_SELECTOR, '.login_submit').click()
七、等待登入成功後獲取cookie
locator = (By.CSS_SELECTOR, '.user') encrypted = '%s****%s' % (phone[:2], phone[-2:]) WebDriverWait(driver, 3).until(EC.text_to_be_present_in_element(locator, encrypted)) cookies = driver.get_cookies() Path('./cookies.txt').write_text(json.dumps(cookies)) print('登入成功') driver.quit()
八、使用儲存的cookie請求其他介面
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62'} cookies = Path('./cookies.txt').read_text() cookies = {x['name']: x['value'] for x in json.loads(cookies)} try: r = requests.get(url, data=data, headers=headers, cookies=cookies, timeout=5) return r.json() except requests.exceptions.Timeout: print('Error: request timeout.') return None except json.decoder.JSONDecodeError: print('Error: json decode error.') return None
九、selenium 的其他操作
https://www.selenium.dev/zh-cn/documentation/webdriver/browser_manipulation/
完。