Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Writing in the front

The background of the story is…. Yeah, this, this, and this, and finally, I felt bullied, and I had to bomb his QQ, yeah

This blog is written out of fear of being banned, after all the results are not very friendly, of course for the bullied programmers, do it again

The core library for the following content is PyNput. This core library can monitor and control our keyboard and mouse, which seems to be a great tool for crawlers

Official website: pypi.org/project/pyn…

Query the API documentation in detail, you can refer to pynput. Readthedocs. IO/en/latest/I…

Evaluating a library, once again, depends directly on whether the documentation is good or not.

Encoding time

The library installation is very simple, slow network speed, the old way, switch source, a little bit, installation success

pip install pynput -i pypi.tuna.tsinghua.edu.cn/simple

After the test code is installed, the library can be tested to see if it works properly. The result is to get the coordinates of the current mouse

from pynput.mouse import Button, Controller

mouse = Controller()

Get mouse coordinates
print('The current mouse coordinate is {0}'.format(
    mouse.position))
Copy the code

The results shown below are completely normal

The current mouse coordinates are (793, 387)

Next, will begin, our purpose is to operate QQ dialog box, and then send a message, send a message, keep sending a message, here to encourage you to try, fun.

The most simple code is as follows, logic is very simple, wait for 5 seconds, waiting for you to open the QQ window, and then in an infinite loop constant input, control the mouse to you on the send button, of course this place can be directly put the mouse button, and then click left key, release the left key, repeat, data is sent out

from pynput import keyboard
from pynput.mouse import Button, Controller
import time
time.sleep(5) Easy to open QQ window
my_mouse = Controller()
my_keyboard = keyboard.Controller()

while(1):
    my_keyboard.type("I'm the most beautiful woman in the world.")
   
    my_mouse.position = (1350.902)
    my_mouse.press(Button.left)
    my_mouse.release(Button.left)
    time.sleep(5)


Copy the code

As shown below, you can directly place the mouse over the send button, and then continuously send data out

A very simple code, to achieve our ultimate goal

If you go with win32GUI edible effect is better, very humanized.

Pynput extension point

The content sent can be customized and added randomness, the effect is better, the sending time can be freely controlled, can be fast or slow. The tools have been provided to you, and it is up to you to test yourself when and where to use them.

Does it look like the Python version of the keystroke Sprite, which does everything a Sprite can do and looks more like a programmer’s way of solving problems?

In the above code, the coordinates of the mouse moving different computer position is different, need to measure oh, what will not amount, screenshots tool will not use??


A lot of times, we can’t solve technology problems, not because we don’t have good technology, but because we don’t know how to do it

This article hopes you hear today that there is a PyNput library out there.

Blogger ID: Dream eraser, hope you like, comment, favorites.