定时给微信好友发送信息

 1 from __future__ import unicode_literals
 2 import requests
 3 import itchat
 4 import time
 5 
 6 def get_news():
 7     url='http://open.iciba.com/dsapi'
 8     resp=requests.get(url)
 9     contents=resp.json()['content']
10     translation=resp.json()['translation']
11     return contents,translation
12 
13 def send_news():
14     try:
15         #登录你的微信账号,会弹出网页二维码,扫描即可
16         itchat.auto_login(hotReload=True)
17         #获取你对应的好友备注,
18         my_friends=itchat.search_friends(name=u'Ray')
19         #获取对应名称的一串数字
20         lover_name=my_friends[0]['UserName']
21         img = '/home/chen/projects/crawls/crawl_download/imags/斗图/9150e4e5gy1frctydjenij204s0500sn.jpg'
22         #获取字典的内容
23         for i in range(11):
24             message1 = 'sb'
25             time.sleep(2)
26             itchat.send(message1, toUserName=lover_name)
27             itchat.send_image(img,toUserName=lover_name)
28             # message2 = 'sb'
29             # message3 = 'sb'
30         # message1=str(get_news()[0])
31         # content=str(get_news()[1][17:])
32         # message2=str(content)
33         # message3='来自爱你的人'
34         #发送消息
35         # itchat.send(message1, toUserName=lover_name)
36         # itchat.send(message2, toUserName=lover_name)
37         # itchat.send(message3, toUserName=lover_name)
38         #没86400秒(1天)发送一次
39         # t=time(86400,send_news())
40         # t.start()
41     except:
42         message4=u'今天最爱你的人出现了 bug /ToT/~~'
43         itchat.send(message4,toUserName=lover_name)
44 def main():
45     send_news()
46 if __name__ == '__main__':
47     main()
原文地址:https://www.cnblogs.com/ray-mmss/p/10080975.html