美文网首页大数据 爬虫Python AI SqlPython小哥哥
520特别版Python实战:教你用微信每天给TA说晚安!

520特别版Python实战:教你用微信每天给TA说晚安!

作者: 14e61d025165 | 来源:发表于2019-05-17 16:06 被阅读1次

    导读:明天就是520,不知你是否已经准备好要表白的话语。为了助力你撩妹成功,大数据今天也提前备了点干货——教你用Python每天给妹纸发晚安。

    没错,每天!长期的坚持显然要比某一天的突击更有诚意!

    用对了Python,520每天都过!

    这次真的是“面向对象编程”!

    但凡一件事,稍微有些重复。我就考虑怎么样用程序来实现它。

    这里给各位程序员朋友分享如何每天给朋友定时微信发送“晚安”,故事,新闻,等等……

    最好运行在服务器上,这样后台挂起来更方便。

    准备:

    Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。

    微信号

    pip install wxpy

    pip install requests

    代码如下:

    不要抄下源码就运行,你需要改动几个地方

    from future import unicode_literals

    from threading import Timer

    from wxpy import *

    import requests

    bot = Bot()

    bot = Bot(console_qr=2,cache_path="botoo.pkl")

    这里的二维码是用像素的形式打印出来!,如果你在win环境上运行,替换为

    bot=Bot()

    def get_news1():

    获取金山词霸每日一句,英文和翻译

    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:
    
        my_friend = bot.friends().search(u'徒手敬岁月')[0]
    
         #你朋友的微信名称,不是备注,也不是微信帐号。     
    
        my_friend.send(get_news1()[0])
    
        my_friend.send(get_news1()[1][5:])
    
        my_friend.send(u"来自爸爸的心灵鸡汤!")
    
        t = Timer(86400, send_news)
    
         #每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧   
    
        t.start()
    
     except:
    
        my_friend = bot.friends().search('常念')[0]
    
     #你的微信名称,不是微信帐号。
    
        my_friend.send(u"今天消息发送失败了")
    
     if __name__ == "__main__":
    
    send_news()
    

    最终效果是这样的:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558080270868 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558080270870 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image> <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558080270873 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    总结:

    代码让生活更美好!

    相关文章

      网友评论

        本文标题:520特别版Python实战:教你用微信每天给TA说晚安!

        本文链接:https://www.haomeiwen.com/subject/doeoaqtx.html