微信企業號應用自動定時打卡考勤——selenium與修改user-agent請求頭

selenium與修改user-agent請求頭

爬微信的時候部分內容受限,例如下圖鏈接,微信企業號應用考勤打卡只接受微信客戶端打開,僅需修改請求頭中的user agent、僞裝微信瀏覽器。

修改user agent的3種方法:

  1. 瀏覽器擴展程序/插件:chrome網上應用商店需要翻牆,較爲困難;網上下載的user-agent-switcher也可能由於瀏覽器版本不同而導致無法使用。火狐應用商店可以下載插件,但試了幾個都不能自定義。
  2. F12網頁開發者工具:More tools-Network conditions-下方User agent取消勾選slect automatically-Custom…-填入需要的user agent。但每次開新的網頁需要重新勾選。
  3. 直接代碼修改:selenium中的chrome webdriver只需新增一行chromeOptions.add_argument(‘相應user agent’);如果是request或scrapy還可以設定請求頭中的其他參數。

微信user agent參考:
https://zhangzifan.com/wechat-user-agent.html
https://blog.csdn.net/weixin_46767352

“var isWeixin = ua.indexOf('micromessenger') != -1;”:user-agent中若包含micromessenger則判斷爲微信瀏覽器,否則一般瀏覽器打不開

代碼:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import schedule
import time

def parse_page():
    chromeOptions = webdriver.ChromeOptions() 
    chromeOptions.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2"') #修改user agent

    browser = webdriver.Chrome(chrome_options=chromeOptions) # browser = webdriver.Chrome()
    #browser.set_page_load_timeout(10)
    wait = WebDriverWait(browser, 10) # 設置隱式等待時間

    browser.get('https://m.ruc.edu.cn/uc/wap/login?redirect=https%3A%2F%2Fm.ruc.edu.cn%2Fuc%2Fapi%2Foauth%2Fdefault%3Fredirect%3Dhttp%253A%252F%252Fxs.ruc.edu.cn%252Ffx%252Findex%252Fcallback%26appid%3D200200519175100346%26state%3Dhttp%253A%252F%252Fxs.ruc.edu.cn%252Ffx%252Fstudents%252Fjump') #網址
    
    # 學號
    name_input = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[2]/div[1]/input')))
    name_input.clear()
    name_input.send_keys('學號')
    
    # 密碼
    code_input = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[2]/div[2]/input')))
    code_input.clear()
    code_input.send_keys('密碼')

    # 登錄
    submit = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[3]')))
    submit.click()
    
    # 確定
    submit = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/section[2]/div[2]/a')))
    submit.click()

    # 體溫
    temp_input = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/div[2]/form/div/div/label[1]/input ')))
    temp_input.clear()
    temp_input.send_keys('36')

    # 打卡
    submit = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/div[2]/form/div/div/input')))
    submit.click()

    # 清除緩存,退出
    #browser.quit()
        
    # 定時   
schedule.every().day.at("07:01").do(parse_page)

while True:
    schedule.run_pending()
    time.sleep(1)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章