可自動切換 learnku 夜間 / 白天模式的 python 指令碼

TNT_God發表於2020-04-29
from lxml import etree
import requests
from datetime import datetime
s = requests.Session()

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'
}
s.headers.update(headers)
text = s.get('https://learnku.com').text
html = etree.HTML(text)
login_token = html.xpath('//meta[@name="csrf-token"]/@content')
payload = {
    '_token':login_token,
    'remember': 'yes',
    'return_back': 'yes',
    'username': '你的賬號名',
    'password': '你的賬號密碼'
}
s.post('https://learnku.com/auth/login', data=payload)
text = s.get('https://learnku.com').text
html = etree.HTML(text)
token = html.xpath('//meta[@name="csrf-token"]/@content')
url = 'https://learnku.com/users/settings/night_mode'
data = {'_method':'POST',
        '_token':token}
while True:
    now = datetime.now()
    hour = now.hour
    day_or_night = html.xpath('/html[@lang="zh"]/@class')[0]
    if hour > 18 or hour < 4:
        if day_or_night == '':
            s.post(url,data=data)
    else:
        if day_or_night == 'night-node':
            s.post(url,data=data)
本作品採用《CC 協議》,轉載必須註明作者和本文連結

coder Derek

相關文章