Log in python WeChat automatic reply (very detailed)

To trouble copying the original source Remarks good! ! !

 

Recently implemented some simple play micro believe we can through the web version of the micro-channel micro-letter web version , log on to sweep the yard grab bag crawling information, you can also post to send a message.

 

"" This library is installed itchat     pip install itchat

First-come-period simple trial so as to log micro letter, run the following code generates a two-dimensional code, after the scan code the phone side to confirm login, it will send a message to 'filehelper', this filehelper is the file on the micro-channel transmission assistant.

 

Import itchat 

# log 
itchat.login ()
 # Send message 
itchat.send (U ' Hello duck! ' , ' filehelper ' )

It will give the file transfer assistant automatically send Hello Duck!

In addition to log on and send a message so we can play, go down ~

 

"" Achieve micro-letter friends male to female ratio

Want sex ratio statistics under their own micro-letter friends, of course, is very simple, to get the buddy list, the list of gender statistics count

Import itchat 

# to be logged in 
itchat.login () 

# get a buddy list 
Friends = itchat.get_friends (Update = True) [0:] 

# initialize counters, men and women, of course, some people are not filled 
male = female = other = 0 

# traversing the list, the list is his first one, so from their "own" counted after 
# 1 for male, 2 female 
for i in Friends [1 :]: 
    Sex = i [ " Sex " ]
     IF Sex = . 1 = : 
        MALE + =. 1
     elif Sex == 2 : 
        FEMALE + =. 1
     the else : 
        OTHER= 1 + # Total count, calculating the ratio of good ah ~ 
Total = len (Friends [1 :]) # good print result Print (U " male friends:.%. 2F %% " % (a float (MALE) / Total 100 * ))
 Print (U " female friend:.% 2F %% " % (float (fEMALE) / Total 100 * ))
 Print (U " other:.% 2F %% " % (float (oTHER) / Total * 100))




operation result:

"" Realize micro-channel auto-reply

Then to achieve a similar auto-reply on the qq, the principle is receiving a message, a message on the back, at the same time send a file to the assistant, you can view messages in a unified file Assistant.

The code is simple, take a look at

# Coding UTF8 = 
Import itchat 

# automatic reply 
# encapsulated decorators, when the received message is Text, i.e., text message 
@ itchat.msg_register ( ' the Text ' )
 DEF text_reply (MSG):
     # when the message is not sent by their own when 
    IF  not msg [ ' fromusername ' ] == myUserName:
         # send a file to prompt assistant 
        itchat.send_msg (U " [% S] received information of friends @% s:% S \ the n- " % 
                        (time.strftime ( " % D%% Y-M-% H:% M:% S " , time.localtime (MSG [ ' CreateTime '])), 
                         Msg [ ' the User ' ] [ ' the NickName ' ], 
                         msg [ ' Text ' ]), ' filehelper ' )
         # reply to a Friend 
        return U ' [automatic reply] Hello, I am now something is not, one will contact you. \ n have received your message:% S \ n ' % (msg [ ' Text ' ]) 

IF  __name__ == ' __main__ ' : 
    itchat.auto_login () 

    # Get your own UserName 
    myUserName = itchat.get_friends (Update = true) [0] ["UserName"]
    itchat.run()

After running will stay logged in, turn on automatic reply mode, view the phone:

 

 

 

Of course, in addition to text Text information, you can also receive pictures (picture expression package count), voice, business cards, location, and type of information sharing Note (that is, someone tips of news, such as the withdrawal of the message), the decorator written as forms can be realized

@itchat.msg_register(['Map', 'Card', 'Note', 'Sharing', 'Picture','Text'])

 

To trouble copying the original source Remarks good! ! !

carry out!

 

Guess you like

Origin www.cnblogs.com/Wang-jialu/p/10936414.html