Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to make a program to send Wechat automatically

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Python to make the program to send Wechat automatically". In daily operation, I believe many people have doubts about how to use Python to make the program to send Wechat automatically. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use Python to make the program to send Wechat automatically". Next, please follow the editor to study!

Module installation and import

This time, let's do a program to send Wechat automatically to send a message to our girlfriend at 12:00 at night. It can also be regarded as a boyfriend's duty.

We need two modules: apscheduler,pyautogui

The shortcut key Windows+r opens the run control box, enter cmd, enter the command line, enter:

Pip install apschedulerpip install pyautogui

Import:

Import pyautoguifrom datetime import datetimefrom apscheduler.schedulers.blocking import BlockingScheduler # Scheduler blocking the current process # blocking 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 BackgroundSchedulerpyautogui

First of all, let's implement automatic message sending.

Pyautogui is a very powerful library that can manipulate the mouse and keyboard. We will use it to operate the computer automatically.

Let's start with some basic settings:

Pyautogui.PAUSE = 1 # set the interval of each step (seconds) to prevent the operation from being too fast

Then we log on to Wechat and minimize.

Next, we place the mouse over 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 ()) # print coordinates, Point (xylene 148, yearly 879) icon_position = pyautogui.position () # Point (xylene 148, yearly 879)

Open Wechat, select the girlfriend's reply window, place the mouse over the input box, and also get the cursor coordinates, in order to lock the focus to the input box to facilitate input later.

Print (pyautogui.position ()) # print coordinates, Point (x, x, y, 751) entry_position = pyautogui.position () # Point (x, x, 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 (174751)

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

There are two ways to enter text:

Pyautogui.typewrite (['oval,' nasty, 'eBay,' enter'])

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

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

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

Neither of these two ways 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, and the second is the interval between each character

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

Pyautogui.moveTo (icon_position, duration=2) # duration is the duration of execution. 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 to press the interval of each letter, optional

Look at the effect:

Of course, if you have a lot of input and find it troublesome, you can do it by copying and pasting:

Import pyperclippyperclip.copy ('is conducting a Chinese experiment, please ignore it when you see it, let alone scold idiots') # copy pyautogui.hotkey ('ctrl',' v') # press the key combination method, ctrl+v paste pyautogui.press ('enter') # press the key

In this way, we have completed the function of automatically sending Wechat messages.

Apscheduler

APScheduler is a Python library that implements the function of delaying scheduling of Python code to be executed, either once or periodically. 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 task scheduler.start ()

The add_job method passes three parameters here, the first is the function to be executed after the time expires, and the second is the type of trigger. The date trigger is selected here, which is triggered at a specific point in time, and the job task will only be executed once. The third parameter, run_date, is the time of execution. Before this, I have encapsulated the code that automatically sends messages into a main function, which can be called when the time comes.

Complete code import pyautoguiimport pyperclipfrom datetime import datetimefrom apscheduler.schedulers.blocking import BlockingSchedulerdef main (): pyautogui.PAUSE = 0 icon_position = pyautogui.Point (xxxu14879) # taskbar icon location entry_position = pyautogui.Point (xpend174yuan751) # input box location pyautogui.moveTo (icon_position, duration=1) # duration is the duration of execution Optional pyautogui.click (icon_position) pyautogui.moveTo (entry_position, duration=0.7) pyautogui.click (entry_position) pyperclip.copy ('go to bed') pyautogui.hotkey ('ctrl',' v') pyautogui.press ('enter') pyperclip.copy (' stupid pig') pyautogui.hotkey ('ctrl',' v') pyautogui.press ('enter') scheduler = BlockingScheduler () # instantiate scheduler.add_job (main 'date', run_date=datetime (2021, 8, 18, 24, 00, 00) # add task scheduler.start () result

At this point, the study on "how to use Python to make a program to send Wechat automatically" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report