Python crawler automatically switches the day / night mode of learnku

Time:2021-11-29

prepare

  • Enter the terminal and enterpip install requests,lxmlTo download the requests crawler library and the lxml (call XPath) module.
  • Set your computer time to regular time

technological process

Python crawler automatically switches the day / night mode of learnku

  1. Log in to learnku using the session method.

  2. Enter the cycle of viewing the current time

  3. Judge the current time

  4. Turn on the night mode from 18:00 to 4:00 a.m. and turn on the day mode at other times

1. Log in to learnku

Since learnku will make a small change to our cookies and tokens every 24 hours, we don’t need the cookie login method. Here, we just use session to login (the method of account and password).

If you want to learn more about sessions and cookies, please refer toUsing requests to simulate user login under Python 3

from lxml import etree

Breakpoint 1: create a session object.

Breakpoint 2: set the user agent of the session session toMozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36, mainly for anti climbing.

Breakpoint 3: this code finds the parameter token required by the login interface.

Breakpoint 4: to login interfacehttps://learnku.com/auth/loginMake a request (login).

By the way, before calling to switch the day / night mode, you have to find out to use the session to find the new token again.

token = html.xpath('//meta[@name="csrf-token"]/@content')

2. Request to switch the day / night mode interface

code:

url = 'https://learnku.com/users/settings/night_mode'
data = {'_method':'POST',
        '_token':token}
s.post(url,data=data)

3. Datetime get the current time

Among the countless time modules, the datetime module is the one that makes me most satisfied. Datetime is a built-in module, which is very convenient to use, so I highly recommend you to learn it. If you want to learn more about the datetime module, take a lookDatetime tutorial of Python 3 standard library

Back to our theme

First, we will import the datetime module.

import datetime

Then we can make an eternal cycle to judge time.

while True:

Breakpoint: check whether the current mode is day or night.

Full code:

from lxml import etree

This work adoptsCC agreement, reprint must indicate the author and the link to this article

coder Derek

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]