Recently, I began to learn Python language, which was quite interesting to be honest, so I played Python at home on the weekend and automatically climbed the hot list on Weibo and sent it to my friends on wechat group. Technology mainly use the python itchat, requests, BeautifulSoup library. Here is the implementation code:
import itchat from itchat.content import TEXT from itchat.content import * import requests from bs4 import BeautifulSoup r=requests.get('https://s.weibo.com/top/summary?cate=realtimehot')
str1='Today's hot news on Weibo \n'
if(r.status_code==200):
r.encoding='utf-8'
bs=BeautifulSoup(r.text,features="lxml")
rank=bs.select('.td-02 a')
for index in range(10):
str1+=str(index+1)+'. '+rank[index].text.strip()+'https://s.weibo.com'+rank[index]['href'] +'\n'
itchat.auto_login(hotReload=True)
itchat.send(u'Hello,world'.'filehelper')
groupName="Ntu Honorary Seniors Group" # Your real wechat group note name.
@itchat.msg_register(TEXT, isGroupChat=True)
def SentChatRoomsMsg(name, context):
itchat.get_chatrooms(update=True)
iRoom = itchat.search_chatrooms(name)
for room in iRoom:
if room['NickName'] == name:
userName = room['UserName']
break
itchat.send_msg(context, userName)
SentChatRoomsMsg(groupName,str1)
print('Sent! ')
Copy the code
Use the code above to ensure that the PIP package installation tool is installed and the import package is installed. If you don’t already, see my other column on how to install the PIP tool!
To implement sending messages to friends, simply add the following function and call:
def sendMsgToFriend(context):
name = itchat.search_friends(name=u'mom')
a = name[0]["UserName"]
itchat.send_msg(context,a)
Copy the code
Finished!