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

要爬取廣點通廣告平臺數據,這平臺竟然沒有提供開發用的API數據接口,登陸是QQ登陸,很複雜,很差抓取登陸接口邏輯php

因而就用這個html

想錄gif的很差錄 -python

文檔:linux

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

python擴展git

https://pypi.org/project/selenium/

selenium的python官方手冊:github

https://selenium-python-zh.readthedocs.io/en/latest/index.html

代碼:web

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


from selenium import webdriver # 從selenium導入webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options import json import time chrome_options = Options() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') # 不啓動界面顯示- linux下命令行模式必須啓用 # chrome_options.add_argument('--headless')
 driver = webdriver.Chrome(chrome_options=chrome_options)  # 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("111") #輸入框輸入
pwdButton.send_keys("xxx") #輸入框輸入
subButton.click() # 顯示等待
try: element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "page")) ) finally: cookies = driver.get_cookies() # time.sleep(5)
    # driver.refresh('http://adnet.qq.com/index')
 with open("cookies.txt", "w") as fp: json.dump(cookies, fp) # 關閉瀏覽器
    driver.close()

 

 

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

 Linux環境搭建json

大體流程ubuntu

一、python

二、pip selenium

三、chrome brower

四、/usr/bin/chromedriver

參考博客:

Linux配置Selenium+Chrome+Python實現自動化測試:
http://zhaoyabei.github.io/2016/08/29/Linux%E9%85%8D%E7%BD%AESelenium+Chrome+Python%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95/

安裝chrom-linux去官網:

https://www.chrome64bit.com/index.php/google-chrome-64-bit-for-linux

下載deb文件到機器上

dpkg -i google-chrome-stable_current_amd64.deb

可是報不少的依賴問題

Unpacking google-chrome-stable (72.0.3626.121-1) ... dpkg: dependency problems prevent configuration of google-chrome-stable: google-chrome-stable depends on fonts-liberation; however: Package fonts-liberation is not installed. google-chrome-stable depends on libappindicator3-1; however: Package libappindicator3-1 is not installed. google-chrome-stable depends on libasound2 (>= 1.0.16); however: Package libasound2 is not installed. google-chrome-stable depends on libatk-bridge2.0-0 (>= 2.5.3); however: Package libatk-bridge2.0-0 is not installed. google-chrome-stable depends on libatk1.0-0 (>= 2.2.0); however: Package libatk1.0-0 is not installed. google-chrome-stable depends on libatspi2.0-0 (>= 2.9.90); however: Package libatspi2.0-0 is not installed. google-chrome-stable depends on libcairo2 (>= 1.6.0); however: Package libcairo2 is not installed. google-chrome-stable depends on libgdk-pixbuf2.0-0 (>= 2.22.0); however: Package libgdk-pixbuf2.0-0 is not installed. google-chrome-stable depends on libgtk-3-0 (>= 3.9.10); however: Package l dpkg: error processing package google-chrome-stable (--install): dependency problems - leaving unconfigured Processing triggers for mime-support (3.59ubuntu1) ... Processing triggers for man-db (2.7.5-1) ... Errors were encountered while processing: google-chrome-stable
View Code
sudo apt-get -f install

解決依賴問題

查看是否已經安裝:

which google-chrome
相關文章
相關標籤/搜索