from __future__ import unicode_literals import requests import itchat import time def get_news(): url = "http://open.iciba.com/dsapi" r = requests.get(url) contents = r.json()['content'] translation = r.json()['translation'] return contents, translation def send_news(): try: # 登陸你的微信賬號,會彈出網頁二維碼,掃描即可 itchat.auto_login(hotReload=True) # 改成你最心愛的人的名字。 my_friend = itchat.search_friends(name=u'要發的人') # 獲取對應名稱的一串數字 XiaoMing = my_friend[0]["UserName"] # 消息設置 message3 = "來自微信自動化測試" # 發送消息 for i in range(1, 10): itchat.send(message3, toUserName=XiaoMing) # t = time(1, send_news()) # t.start() except: message4 = u"/(ㄒoㄒ)/~~" itchat.send(message4, toUserName=XiaoMing) def main(): send_news() if __name__ == '__main__': main()