1. 程式人生 > >使用python的selenium自動化登錄獲取cookie

使用python的selenium自動化登錄獲取cookie

文檔 pytho gin 登錄 not www. ear driver res

要爬取廣點通廣告平臺數據,這平臺居然沒有提供開發用的API數據接口,登錄是QQ登錄,很復雜,不好抓取登錄接口邏輯

於是就用這個

想錄gif的不好錄 -

文檔:

https://www.yiibai.com/selenium/selenium_webdriver.html

代碼:

#!/usr/local/bin/python
# -*- coding: UTF-8 -*-

from selenium import webdriver # 從selenium導入webdriver
import json
import time

driver = webdriver.Chrome()  # Optional argument, if
not specified will search path. driver.get(http://adnet.qq.com/index) # 獲取百度頁面 driver.switch_to.frame(ptlogin_iframe) # 進入iframe # 選擇賬號密碼登錄 selElement = driver.find_element_by_id(switcher_plogin) selElement.click() # 輸入賬號密碼 userElement = driver.find_element_by_id(u) pwdButton = driver.find_element_by_id(
p) #密碼輸入框 subButton = driver.find_element_by_id(login_button) #密碼輸入框 userElement.send_keys("***") #輸入框輸入 pwdButton.send_keys("***") #輸入框輸入 subButton.click() # 讀取cookie time.sleep(5) driver.refresh() cookies = driver.get_cookies() with open("cookies.txt", "w") as fp: json.dump(cookies, fp) # 關閉瀏覽器 driver.close()

很簡潔吧,真真實實能解決登錄這一塊的問題,但是linux上環境的搭建真滴不容易

使用python的selenium自動化登錄獲取cookie