博客文章地址:點我查看
起因
前幾天接觸到linux中的crontab可以定時完成更換壁紙、推送本地文件到github等功能。這幾天想着可不可以弄個自動簽到的腳本,本來用的爬蟲帶cookie實現登錄然後簽到,但是過幾天cookie失效就涼涼了。偶然的機會瞭解到selenium可以模擬瀏覽器點擊,於是找了找關於自動簽到的文章學習下,在次記錄下。
selenium簡介
Selenium 是一個綜合性的項目,爲web瀏覽器的自動化提供了各種工具和依賴包。他有很多功能。此次主要結合Python,功能可以理解爲可以模擬一個瀏覽器並進行各種點擊、填寫賬號密碼等操作。
放張截圖
要點
主要是選取到合適的元素,假裝代碼是一個人,你要先打開網頁,然後輸入賬號,然後你要點擊登錄這樣的按鈕,對於代碼來說它要先找到登錄的按鈕才能進行點擊等操作。所以查找的合適精準至關重要。
查找元素
通過id
driver.find_element_by_id('loginForm')
通過name
driver.find_element_by_name('username')
通過Xpath
#絕對定位 (頁面結構輕微調整就會被破壞)
driver.find_element_by_xpath("/html/body/form[1]")
#HTML頁面中的第一個form元素
driver.find_element_by_xpath("//form[1]")
#包含 id 屬性並且其值爲 loginForm 的form元素
driver.find_element_by_xpath("//form[@id='loginForm']")
對於查找而言,你可以F12選中一個元素然後右鍵選擇Copy便有copy xpath等選項
其他要點
點擊操作
driver.find_element_by_xpath('/html/body/div[2]/button').click()
填寫信息
driver.find_element_by_id('email').send_keys(username)
獲取元素內容
driver.find_element_by_xpath('//*[@id="swal2-content"]').text
缺點
對於selenium我個人感覺最大的缺點就是比較慢,相對於爬蟲速度慢點多,代碼中雖然用到了禁用圖片、設置超時等操作但還是比較慢。不過對於自動簽到而言,慢一點也沒什麼太大的影響,只要能穩定就行。
代碼
"""
功能:自動簽到腳本
配置:登錄地址 + 賬號 + 密碼
"""
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
def checkin(login_url,username,password):
chrome_options = Options() #解決使用chrome報錯
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("enable-automation")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--dns-prefetch-disable")
chrome_options.add_argument("--disable-gpu")
chrome_prefs = {} #禁止加載圖片以提高速度
chrome_options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"images": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"images": 2}
driver = webdriver.Chrome(options=chrome_options, executable_path='/usr/bin/chromedriver') # 初始化chrome
driver.set_page_load_timeout(30) #設置超時以提高速度
driver.maximize_window() # 最大化窗口
try:
driver.get(login_url) # 進入登錄頁面
except:
driver.execute_script("window.stop()") #加載超時停止加載執行下一步操作
print("當前頁面爲:"+driver.find_element_by_xpath('//*[@id="app"]/section/div/div[1]/div/h4/span').text)
print("當前賬號爲:"+username)
print("--------------------------------")
try:
time.sleep(3) # 延時加載
driver.find_element_by_id('email').send_keys(username) # 填充用戶名和密碼
driver.find_element_by_id('password').send_keys(password)
driver.find_element_by_xpath('/html/body/div[1]/section/div/div[1]/div/form/div[4]/button').click() # 登錄
time.sleep(3)
driver.find_element_by_xpath('/html/body/div[2]/div/div/div[3]/button').click() #點擊剛進主頁彈出的彈窗
time.sleep(3) #等待兩秒,點擊read後網頁流量是動態增加的
try: # 未簽到
driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/section/div[1]/div/div/a").click() # 點擊簽到
print(driver.find_element_by_xpath('//*[@id="swal2-content"]').text)
driver.find_element_by_xpath("/html/body/div[7]/div/div[3]/button[1]").click()
print("簽到成功,恭喜你,幸運的boy")
except Exception as e:
print("已經簽到過了")
except Exception as e:
print(e)
print("簽到失敗")
time.sleep(3)
print("當前流量爲:"+driver.find_element_by_xpath("//*[@id='app']/div/div[3]/section/div[3]/div[2]/div/div[2]/div[2]/span").text+"G")
print()
driver.quit()
checkin("url","賬號","密碼")
checkin("url","賬號","密碼")
寫在最後
以上僅爲個人總結,我也是剛接觸,如有錯誤,請聯繫我更正。以後發現更多linux和python有趣的事也會分享的。