The female ticket was noisy at night, using python to coax the female ticket to sleep, the female ticket exclaimed: FUCK, FUCK, FUCK (then the female ticket disappeared, and ran away with python)

My girlfriend asked me to coax her to sleep, and I coaxed him directly. Women only affect the speed of my coding.
But, I'm just too sleepy to make it through... eh? Is it important for a girlfriend to sleep?
Please add image description

Brothers, if you want Uncle Fan's complete high-quality teaching videos, video notes, interview materials, tool template learning materials, you can find our Miss Beibei [mashan-qq] remarks [csdn000] for free

What should we do then? It's time to bring Python to the table!

Python debuts

This time, let's make a program to automatically send WeChat, and send a message to my girlfriend at 12 o'clock in the middle of the night, which can be regarded as fulfilling the obligation of a boyfriend.

install and import

We need two modules: apscheduler , pyautogui

Shortcut Windows+r to open the run control box, enter cmd, enter the command line, enter:

pip install apscheduler
pip install pyautogui

import:

import pyautogui
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler # 阻塞当前进程的调度器
# blocking类型调度器会阻塞当前进程,若你想要后台运行的调度器,可以使用以下代码:
# from apscheduler.schedulers.background import BackgroundScheduler

pyautogui

First let's implement automatic message sending

pyautogui is a very powerful library that can operate mouse and keyboard. We will use it to complete the automation of the computer.

Let's do some basic settings first:

pyautogui.PAUSE = 1 # 设置每一步操作的间隔(秒),可防止操作太快

Then we log in to WeChat and minimize.

Next, we put the mouse on the taskbar icon of WeChat, run the following statement, get the coordinates of the cursor at this time, and return a Point object:

print(pyautogui.position()) # 打印坐标,Point(x=148, y=879)
icon_position = pyautogui.position() # Point(x=148, y=879)

Open WeChat, select the girlfriend's reply window, put the mouse on the input box, and also get the cursor coordinates, in order to lock the focus to the input box to facilitate the input later.
Please add image description

print(pyautogui.position()) # 打印坐标,Point(x=174, y=751)
entry_position = pyautogui.position() # Point(x=174, y=751)

Next, the control program clicks on these two dots in sequence:

pyautogui.click(icon_position) # 默认左键单击
# pyautogui.click(148, 879)
pyautogui.click(entry_position)
# pyautogui.click(174, 751)

After opening WeChat and locking the focus, we start entering text.

There are two ways to enter text:

  • pyautogui.typewrite(['o', 'n', 'e', 'enter'])

    Pass in a list in the method, each element in it is a single letter or a special key

  • pyautogui.typewrite('You can type multiple letters in this way')

    Pass in a string, but not print letters and special keys at the same time.

Neither of these two methods can directly input Chinese, so you can only rely on your input method to input Chinese.

pyautogui.typewrite([*list('zhengzai '), *list('jinxing '), 'shift', *list('pyautogui'), 'shift', *list('shiyan '), 'enter'], 0.1) # 第一个参数是输入文本,第二个是输入每个字符的间隔时间

In order to make our operation more  human-  like, let's add the code for moving the mouse:

pyautogui.moveTo(icon_position, duration=2) # duration为执行时长,可选
pyautogui.click(icon_position)
pyautogui.moveTo(entry_position, duration=2)
pyautogui.click(entry_position)
pyautogui.typewrite([*list('zhengzai '), *list('jinxing '), 'shift', *list('pyautogui'), 'shift', *list('shiyan '), 'enter'], 0.1) # 第二个参数为按下每一个字母的间隔,可选

Take a look at the effect:
Please add image description
Of course, if you want to input a lot of content and it is too troublesome, you can do it by copying and pasting:

import pyperclip

pyperclip.copy('正在进行发中文试验,看到请忽略,更不要骂傻逼') # 复制
pyautogui.hotkey('ctrl', 'v') # 按下组合键的方法,ctrl+v粘贴
pyautogui.press('enter') # 按下按键

Please add image description
In this way, we have completed the function of automatically sending WeChat messages.

apscheduler

APScheduler is a Python library that enables delayed scheduling of Python code to be executed, either once or periodically. New tasks can be added or old tasks can be deleted at any time. It is very convenient to perform scheduled tasks.

scheduler = BlockingScheduler() # 实例化一个调度器
scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 00, 00)) # 添加任务
scheduler.start()

The add_job method passes 3 parameters here, the first is the function to be executed after the time is up, and the second is the type of trigger. The date trigger is selected here, which is triggered at a specific time point, and the job task will only be executed once. The third parameter run_date is the execution time. Before this, I have encapsulated the code for automatically sending the message into the main function, just call it when the time is up.

full code

import pyautogui
import pyperclip
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def main():
	pyautogui.PAUSE = 0

	icon_position = pyautogui.Point(x=148, y=879) # 任务栏图标位置
	entry_position = pyautogui.Point(x=174, y=751) # 输入框位置

	pyautogui.moveTo(icon_position, duration=1) # duration为执行时长,可选
	pyautogui.click(icon_position)
	pyautogui.moveTo(entry_position, duration=0.7)
	pyautogui.click(entry_position)
	pyperclip.copy('快去睡觉')
	pyautogui.hotkey('ctrl', 'v')
	pyautogui.press('enter')
    pyperclip.copy('笨猪')
	pyautogui.hotkey('ctrl', 'v')
	pyautogui.press('enter')
    
scheduler = BlockingScheduler() # 实例化
scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 00, 00)) # 添加任务
scheduler.start()

Done! Now can go to sleep.

result

When I woke up the next morning, I was scolded by my mother and asked me why my computer was still on at 12 midnight and I was still sending WeChat!

However, fortunately, my girlfriend was not lost, and I successfully completed my girlfriend's task!
Please add image description

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/123485628