In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces "how to use Python code to achieve Wechat message withdrawal capture function" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
1. Module introduction
First of all, message recall capture requires a very powerful library on python: itchat. If you haven't used it, the editor would like to introduce:
Project description itchat is an open souce wechat api project for personal account. It enables you to access your personal wechat account through command line.
Itchat is an open source Wechat api project for personal accounts. You can access your personal Wechat account from the command line. So we're going to use this library today, first of all, we need to:
Pip install itchat
2. Familiar with module function
The editor here takes into account that some friends have never used this module, so the following will be a simple explanation of this module.
2.1 how to log in to Wechat
Since we want to capture Wechat's withdrawal information, the first step must be to log in to Wechat. Logging in to Wechat is very simple. It only takes two lines of code:
Import itchatitchat.login ()
This is good, is it very simple, and then after running will appear a QR code, scan and then authorize login on the phone, the console will show whether you have successfully logged in or not.
Login successfully as.
This means that you have successfully logged in, but you cannot log in to the web version of Wechat if it is the newly created Wechat and the Wechat that has not been used for a long time, so it will also cause you to fail to log in. If you can't log in, there's nothing you can do.
2.2 get a friend list
Import itchat itchat.auto_login (hotReload=True) friends = itchat.get_friends () # Friends list print (friends)
Use the get_friends () function to get all the friend information of the friend list, including nickname, memo name, address, personality tag, gender, and so on.
[{'UserName':' @ 7c2215e17edf4b195ecf944abcaf19ba72e3eb24b8442d5e32d4a8be92, 'City':', 'DisplayName':', 'PYQuanPin':', 'RemarkPYInitial':', 'Province':', 'KeyWord':', 'RemarkName':', 'PYInitial':', 'EncryChatRoomId':','* *':', 'Signature':', 'NickName':' *', 'RemarkPYQuanPin':'' 'HeadImgUrl':'* *
Here I casually copied the information of a friend. When it comes to privacy, I replaced all the information with *. We focus on analyzing the content of the information. For example, at the beginning, UserName is the unique identity of the user, which is the same as the ID card number. All friends' UserName are different, and then NickName: this is a friend's nickname. HeadImgUrl: this is the address of my friend's avatar. I won't introduce you one by one. If you are interested, you can find out for yourself.
2.3 send messages to friends
We are now ready to send a message to our friends, how to do it? Look at the following code
Import itchat itchat.auto_login (hotReload=True) itchat.send ("Life is too short, I learn python", toUserName= "@ c4326bda513bf7cdd19f1fa03dbf7e7bc3bbc57e5abb71fd580b2c3c32cddd99")
The itchat.send () function can take two parameters, the first is the message you want to send to a friend, and the second specifies the friend, that is, the unique ID UserName mentioned above in toUserName=, but I think the above method is still a little inappropriate, and then I improved it.
Import itchat itchat.auto_login (hotReload=True) friends = itchat.get_friends () nickName = 'you are bald and I am not bald' for i in friends: if 'you are bald and I am not bald' = I ['NickName']: itchat.send (' Life is too short, I will learn python', toUserName=i ['UserName']) break
In this way, I can send a message to any friend. Look it up in the friend's nickname, ah, in the friend list. If I find it, I will get the friend's UserName and send a message. I can also find it through the friend's remark name RemarkName, and you can choose according to your preference.
2.4 Decoration
On the itchat module there are many features, Xiaobian here do not explain too much, we just need to understand the message to withdraw the need for knowledge on the line, and then we carry on the last content, decorator on the decorator editor here a brief introduction, decorator is the courage to expand the original function of a function, the purpose is not to change the name of the original function to add new features to the function. For example, I like Anzhi another function foo (), you do not know the implementation principle of the function, you certainly can not modify the code of this function, but you need to add a function to this function that outputs the start and end run time, and how to implement it, then you can use the decorator:
Import timedef show_time (foo): def inner (): print (time.time) foo () print (time.time ()) return inner @ show_timedef foo (): passfoo ()
The meaning of the above code: first @ show_time is to use a decorator show_time. At this time, the decorator's function, that is, foo (), will be passed to the decorator show_time () as an argument. We know that if the function is returned, it actually executes the function, so the program executes the internal function inner (), which outputs the start time, and then calls the foo () function. Finally, the output end run time, so as to achieve a functional extension, which is also a typical aspect-oriented programming idea.
3. How to monitor the information withdrawn by your friends
In fact, at this point, we have completed the monitoring of the message and only need to modify it a little bit, but there is still a problem with this program, that is, we need to save all the messages. We can see directly that the message sent by our friends is not superfluous. Our purpose is to know what our friends have withdrawn. Here is the question of how to monitor whether a friend withdraws the information. In fact, it is not difficult. The Content module provides us with a NOTE type, which refers to the system message, so we can customize a function to listen to the system message:
Import itchat from itchat.content import * # Import content module itchat.auto_login (hotReload=True) @ itchat.msg_register (NOTE) def note_info (msg) under itchat: # listen for system messages print (msg) itchat.run ()
To run the program, we withdraw a message to test it, and the output is as follows:
'DisplayName':', 'ChatRoomId': 0,' KeyWord':', 'EncryChatRoomId':', 'IsOwner': 0} >,' Type': 'Note',' Text': 'you withdrew a message'}
The content of the intercepted part of the example will send the text content of the small withdrawal message "you have withdrawn a message". If you want to know whether your friend has withdrawn the message, it is very simple to judge, msg ['Text'] =' you have withdrawn a message'.
4. Implement Wechat message withdrawal capture function
Now that the code for each step of the program has been analyzed, the next step is a summary of all the code. Here is a summary of all the code:
Now let's test it. First of all, I asked one of my two friends to send me a message:
The results obtained are:
This is the end of the content of "how to use Python code to achieve Wechat message withdrawal and capture function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.