思路整理:1、進入心靈雞湯網頁,使用python獲取心靈雞湯內容
2、登陸微信,找到需要發送的朋友
3、發送獲取的內容
1、獲取心靈雞湯的內容
如下圖,獲取第一條雞湯
實現如下:
2、登陸微信,搜索朋友,進行發送
1 import requests 2 import wxpy 3 from bs4 import BeautifulSoup 4 5 # 微信網頁登陸 6 bot = wxpy.Bot(console_qr=2,cache_path='botoo.pkl') 7 8 # 獲取心靈雞湯中的最新內容,可以參考其他爬蟲隨便查看怎么爬蟲 9 def get_msg(): 10 url = 'http://www.59xihuan.cn/index_1.html' 11 h = requests.get(url) 12 html = h.text 13 news_bf = BeautifulSoup(html,"html.parser") 14 msg = news_bf.find('div', class_='pic_text1') 15 news = msg.text 16 # print(msg) 17 # print(news) 18 return news 19 20 # 給朋友發送消息 21 def send_msg(): 22 try: 23 # 添加朋友微信昵稱 24 friend = bot.friends().search(u'xxxxx')[0] 25 friend.send(get_msg()) 26 29 except:pass 30 31 32 if __name__ == '__main__': 33 send_msg()
其他發送類型格式:
發送文本消息:friend.send('文本消息')
發送圖片消息:friend.send_image('圖片消息.jpg')
發送視頻消息:friend.send_video('視頻消息.mov')
發送文件消息:friend.send_file('文件消息.zip')
以動態的方式發送圖片:friend.send('@img@圖片消息.jpg')
朋友收到的消息:
有興趣的可以參考筆者的爬取小說隨筆,爬下心靈雞湯的多條雞湯和圖片,然后發送朋友雞湯和配圖
如果惡搞的話可以去詳細了解wxpy的使用,每秒發送朋友x條內容,持續y時間