程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

My girlfriend asked me to urge her to sleep at 12 p.m. and I solved it easily with Python

編輯:Python

Here's the thing : Last night, , My girlfriend asked me to rush her to bed at twelve .

however , But I'm so sleepy , I can't stand it …… Is that so? ? It's important for a girlfriend to sleep ?

but , Girlfriend's orders , I dare not disobey ……

But you can't sleep without !

What should we do at this time ? It's time to let Python Debut !

Python On stage

This time, let's do a program to automatically send wechat , Send a message to your girlfriend at midnight , I've done my duty as a boyfriend .

Install and import

We need two modules :apscheduler,pyautogui

Shortcut key Windows+r Open the operation control box , Input cmd, Go to the command line , Input :

pip install apscheduler
pip install pyautogui

Import :

import pyautogui
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler # The scheduler that blocks the current process
# blocking The type scheduler blocks the current process , If you want a scheduler running in the background , You can use the following code :
# from apscheduler.schedulers.background import BackgroundScheduler

pyautogui

First, let's implement automatic message sending

pyautogui It's a very powerful library , Can operate the mouse and keyboard . We will use it to automatically operate the computer .

Let's do some basic settings first :

pyautogui.PAUSE = 1 # Set the interval between each step ( second ), It can prevent the operation from being too fast

Then we log in to wechat , To minimize the .

Next, we put the mouse on the taskbar icon of wechat , Run the following statement , Get the coordinates of the cursor at this time , Return to one Point object :

print(pyautogui.position()) # Print coordinates ,Point(x=148, y=879)
icon_position = pyautogui.position() # Point(x=148, y=879)

Open the WeChat , Select your girlfriend's reply window , Place the mouse over the input box , Also get the cursor coordinates , In order to lock the focus to the input box for later input .

print(pyautogui.position()) # Print coordinates ,Point(x=174, y=751)
entry_position = pyautogui.position() # Point(x=174, y=751)

Next , The control program clicks these two points in turn :

pyautogui.click(icon_position) # Default left click
# pyautogui.click(148, 879)
pyautogui.click(entry_position)
# pyautogui.click(174, 751)

After opening wechat and locking the focus , Let's start typing text .

There are two ways to enter text :

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

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

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

Incoming string , But you can't print letters and special keys at the same time .

Neither of these two methods can input Chinese directly , 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) # The first parameter is the input text , The second is to enter the interval of each character

In order to make our operation more pretending to be what one is not Like human operation , Let's add the code for moving the mouse :

pyautogui.moveTo(icon_position, duration=2) # duration Is the execution time , Optional
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) # The second parameter is the interval between pressing each letter , Optional

Look at the effect :

Of course , If you have to enter a lot of content , I don't like trouble , You can do this by copying and pasting :

import pyperclip
pyperclip.copy(' The experiment of sending Chinese is under way , See, please ignore , Don't scold a fool ') # Copy
pyautogui.hotkey('ctrl', 'v') # The method of pressing the key combination ,ctrl+v Paste
pyautogui.press('enter') # Press the button

such , We have completed the function of automatically sending wechat messages .

apscheduler

APScheduler It's a Python library , Delay scheduling can be implemented to execute Python Function of code , It can be executed only once , It can also be performed on a regular basis . You can add new tasks or delete old tasks at any time . It is very convenient to carry out scheduled tasks .

scheduler = BlockingScheduler() # Instantiate a scheduler
scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 00, 00)) # Add tasks
scheduler.start()

add_job The method is passed here 3 Parameters , The first is the function to be executed after the time , The second is the type of trigger . The choice here is date trigger , Trigger at a specific point in time , Job tasks will only be performed once . The third parameter run_date Is the time of execution . Before that, I have encapsulated the code of automatically sending messages as main function , It only needs to be called after that time .

Complete 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) # Taskbar icon location
entry_position = pyautogui.Point(x=174, y=751) # Input box position
pyautogui.moveTo(icon_position, duration=1) # duration Is the execution time , Optional
pyautogui.click(icon_position)
pyautogui.moveTo(entry_position, duration=0.7)
pyautogui.click(entry_position)
pyperclip.copy(' Go to sleep quickly ')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
pyperclip.copy(' Stupid pig ')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
scheduler = BlockingScheduler() # Instantiation
scheduler.add_job(main, 'date', run_date=datetime(2021, 8, 18, 24, 00, 00)) # Add tasks
scheduler.start()

It's done ! Now you can go to bed .

result

Get up the next morning , I was scolded by my mother , Ask me why midnight 12 The computer was still on when I was ten o'clock , And I'm still sending my own wechat !

however , Fortunately, I didn't lose my girlfriend , I successfully completed my girlfriend's task !

About Python Technology reserve

Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !

One 、Python Learning routes in all directions

Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .

Two 、 Learning software

If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .

3、 ... and 、 Getting started video

When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .

Four 、 Practical cases

Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .

5、 ... and 、 Interview information

We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .


This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved