To be honest, this method is very practical, and the code from a year ago can still be used normally. But when I bought a server at the beginning of this year, I thought about migrating all the scheduled tasks of these local computers to the server. Going back to the previous code at this time, using Selenium as our automatic check-in solution seems a bit "clunky".
Today, I want to share with you the upgrade - the request method.
specific methods
First open the website we need to check in, use the Chrome browser's F12 shortcut (or "inspect" through the right-click menu) to open the browser's developer tools, and click Network to view all loaded requests.
As shown in the animation below, at this time, when we manually click to sign in, we will find that a new post request (checkin) has appeared. Unsurprisingly, this is our sign-in request.
What we have to do is to convert the request into Python code. The specific method is also very simple. For details, please refer to the previous article: An artifact that every crawler should know! Among them, we share two methods that can convert the curl request command to the Python request code with one click.
Go back to the new request found at the beginning of the previous article, as shown in the figure below, right-click to copy the network request captured by the network as cURL (bash).
Then open the website: https://curl.trillworks.com/
Fill in the content just copied on the left side of the website, and the corresponding python requests
code .
import requests
headers = {你自己的headers}
cookies = {你自己的cookies}
response = requests.post('https://*********.org/user/checkin', cookies=cookies, headers=headers)
The rest is very simple, you only need to execute the code regularly every day (simulating a post check-in request) to realize the website check-in.
Compared with the previous method, the biggest advantage is that the code is more compact and the execution time is shorter. Selenium needs to simulate the browser to load and click to realize automatic sign-in. Even if you use explicit wait and other operations, it can only be controlled for about 10 seconds.
In addition to adding timing functions, this sign-in code can also add notification functions (such as reminding yourself of successful sign-in + sign-in results through WeChat/Feishu/DingTalk).
├──书籍《快学Python:自动化办公轻松实战》
│ ├── 9.3.4 案例 :Python 机器人定时发送消息
│ ├── 9.4.4 案例 :利用 Python 制作飞书机器人
The cases in the book are all derived from real needs, and I do it myself. The timed task can be written like this:
import schedule
import requests
headers = {你自己的headers}
cookies = {你自己的cookies}
def job():
res = requests.post('https://*********.org/user/checkin', cookies=cookies, headers=headers)
schedule.every().day.at("09:50").do(job)
while True:
schedule.run_pending()
time.sleep(1)
For example, the website sign-in method I introduced to you today has been running normally on my server for more than half a year, and my Feishu can receive a sign-in success reminder every morning at 9.30.