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

Analysis of the example of operating Wechat with Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail the example analysis of using Python to operate Wechat. The editor thinks it is quite practical, so I share it with you for reference. I hope you can get something after reading this article.

For learning reference only, get a friend list.

WechatPCAPI provides the method get_friends (), which returns information including a list of friends, groups and official accounts, mainly including WeChat accounts, nicknames and notes set by yourself.

We use the acquired nickname to make a simple word cloud display, and the code implementation is as follows:

Logging.basicConfig (level=logging.INFO)

Def on_message (message):

Pass

Def get_friends ():

# initialize Wechat instance

Wx_inst = WechatPCAPI (on_message=on_message, log=logging)

# launch Wechat

Wx_inst.start_wechat (block=True)

# waiting for a successful login, you need to manually scan the code to log in to Wechat

While not wx_inst.get_myself ():

Time.sleep (5)

Print ('login succeeded')

Nicknames = []

# excluded words

Remove = ['still','no', 'some','so', 'sure enough'

'get up', 'things', 'Why', 'really','so'

'but', 'how', 'still', 'time', 'one'

"what", "oneself", "everything", "appearance", "same"

"No", "No", "one", "this", "for"

]

For key, value in wx_inst.get_friends () .items ():

If key in ['fmessage',' floatbottle', 'filehelper'] or' chatroom' in key:

Continue

Nicknames.append (value ['wx_nickname'])

Words = []

For text in nicknames:

If not text:

Continue

For t in jieba.cut (text):

If t in remove:

Continue

Words.append (t)

Global word_cloud

# separate words with commas

Word_cloud =', '.join (words)

Def nk_cloud ():

# Open the word cloud background image

Cloud_mask = np.array (Image.open ('bg.png'))

# define some attributes of word cloud

Wc = WordCloud (

# background image segmentation color is white

Background_color='white'

# background pattern

Mask=cloud_mask

# display the maximum number of words

Max_words=300

# display Chinese

Font_path='./fonts/simkai.ttf'

# maximum size

Max_font_size=70

)

Global word_cloud

# word cloud function

X = wc.generate (word_cloud)

# generate word cloud pictures

Image = x.to_image ()

# Show word cloud pictures

Image.show ()

# Save word cloud pictures

Wc.to_file ('nk.png')

Take a look at the effect:

Message revocation prevention

When we use Wechat to chat with our friends, the other party will sometimes withdraw the message. Normally, we do not know what the message withdrawn by our friend is. Through WechatPCAPI, we can achieve the function of anti-withdrawal of the message.

We know that the message that is usually withdrawn is the content sent in the previous step of the click recall operation, of course, it is possible to withdraw the first two or three steps. Here we only deal with the message of withdrawing the previous step. The basic idea is: we will save the message sent in the previous step, and when the other party clicks on the withdraw operation, we will return the message of the previous step to ourselves again.

Take a look at the implementation code:

Logging.basicConfig (level=logging.INFO)

Queue_recved_event = Queue ()

Def on_message (msg):

Queue_recved_event.put (msg)

Def login ():

Pre_msg =''

# initialize Wechat instance

Wx_inst = WechatPCAPI (on_message=on_message, log=logging)

# launch Wechat

Wx_inst.start_wechat (block=True)

# waiting for a successful login, you need to manually scan the code to log in to Wechat

While not wx_inst.get_myself ():

Time.sleep (5)

Print ('login succeeded')

While True:

Msg = queue_recved_event.get ()

Data = msg.get ('data')

Sendinfo = data.get ('sendinfo')

Data_type = str (data.get ('data_type'))

Msgcontent = str (data.get ('msgcontent'))

Is_recv = data.get ('is_recv')

Print (msg)

If data_type = ='1' and 'revokemsg' not in msgcontent:

Pre_msg = msgcontent

If sendinfo is not None and 'revokemsg' in msgcontent:

User = str (sendinfo.get ('wx_id_search'))

Recall = 'withdrawn message:' + pre_msg

Wx_inst.send_text (to_user=user, msg=recall)

Take a look at the effect of the operation:

This is the end of the article on "sample analysis of using Python to operate Wechat". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Internet Technology

Wechat

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

12
Report