• 第一步,也是最难的一部

    • 首先得要有个女朋友

    

  • 利用python的第三方库wxpy来登录微信,实现消息发送功能

     from wxpy import *
    
     def login():
    bot = Bot(cache_path=True)
    my_lover = bot.friends().search('夏叶')[0]
    return my_lover

    search方法接收一个昵称的字符串,它会返回一个查找到的所有条件的列表对象,我们这里只有这一个人,直接选第一个对象

  • 去天气网爬取网页数据,将关于天气的信息筛选下来,整合成消息字符串
     import requests
    from lxml import etree def get_page(url):
    r = requests.get(url)
    r.encoding = r.apparent_encoding
    return r.text if r.status_code == 200 else None def parse_page(html):
    html = etree.HTML(html)
    forecasts = html.xpath('/html/body/div[8]/div[1]/div[1]/div[2]/ul/li/a[1]/@title')
    forecasts = '\n'.join(forecasts)
    keys = html.xpath('/html/body/div[8]/div[2]/div[6]/ul/li/b/text()')
    values = html.xpath('/html/body/div[8]/div[2]/div[6]/ul/li/a/p/text()')
    day_info = {i: values[keys.index(i)] for i in keys}
    message = '青哥哥今日提醒:\n\n' + '南京今日生活指数:\n' + '\n'.join(
    ['{}: {}'.format(i, day_info[i]) for i in day_info]) + '\n' * 3 + '南京主要地区天气预报:\n' + forecasts
    return message

    爬虫库使用的是requests,解析库用的是xpath,最后将字符串拼接,返回消息对象

  • 获取当前时间和设置闹钟
     from datetime import datetime
    
     def get_time():
    time = datetime.now().strftime('%H:%M:%S')
    return time clock = '06:00:0{}' # 设置启动时间
    interval = 3 # 设置时间间隔
    time_zone = [clock.format(i) for i in range(interval)]

    防止电脑性能过差或cpu使用率过高导致的时间漏缺,设置一下时间间隔,我这里设置的是三秒,最后将设置时间区间

  • 主函数发送消息
     def main(my_lover):
    url = 'http://www.tianqi.com/nanjing/'
    html = get_page(url)
    message = parse_page(html)
    my_lover.send(message)

    不同城市的url可以去天气网自定义,一般网页的结构是不会变得

  • 判断时间,最后的完整代码
     from time import sleep
    from datetime import datetime
    from wxpy import *
    import requests
    from lxml import etree def login():
    bot = Bot(cache_path=True)
    my_lover = bot.friends().search('夏叶')[0]
    return my_lover def get_page(url):
    r = requests.get(url)
    r.encoding = r.apparent_encoding
    return r.text if r.status_code == 200 else None def parse_page(html):
    html = etree.HTML(html)
    forecasts = html.xpath('/html/body/div[8]/div[1]/div[1]/div[2]/ul/li/a[1]/@title')
    forecasts = '\n'.join(forecasts)
    keys = html.xpath('/html/body/div[8]/div[2]/div[6]/ul/li/b/text()')
    values = html.xpath('/html/body/div[8]/div[2]/div[6]/ul/li/a/p/text()')
    day_info = {i: values[keys.index(i)] for i in keys}
    message = '青哥哥今日提醒:\n\n' + '南京今日生活指数:\n' + '\n'.join(
    ['{}: {}'.format(i, day_info[i]) for i in day_info]) + '\n' * 3 + '南京主要地区天气预报:\n' + forecasts
    return message def get_time():
    time = datetime.now().strftime('%H:%M:%S')
    return time clock = '06:00:0{}' # 设置启动时间
    interval = 3 # 设置时间间隔
    time_zone = [clock.format(i) for i in range(interval)] def main(my_lover):
    url = 'http://www.tianqi.com/nanjing/'
    html = get_page(url)
    message = parse_page(html)
    my_lover.send(message) if __name__ == '__main__':
    my_lover = login()
    print('waiting......')
    while True:
    time = get_time()
    if time in time_zone:
    main(my_lover)
    print(time)
    sleep(interval)
    sleep(1) # 程序休眠一秒,减少cpu的压力

python定时发信息给女友的更多相关文章

  1. 利用python itchat给女朋友定时发信息

    利用itchat给女朋友定时发信息 涉及到的技术有itchat,redis,mysql,最主要的还是mysql咯,当然咯,这么多东西,我就只介绍我代码需要用到的,其他的,如果需要了解的话,就需要看参考 ...

  2. Python定时框架 Apscheduler 详解【转】

    内容来自网络: https://www.cnblogs.com/luxiaojun/p/6567132.html 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序, ...

  3. 用python定时文章发布wordpress

    用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...

  4. 新发现:AirDroid(用Web端控制自己的手机发信息)

    http://web.airdroid.com/ 好多功能呀,有空研究研究 http://jingyan.baidu.com/article/b24f6c82cd4ade86bfe5daf3.html ...

  5. python定时利用QQ邮件发送天气预报

    大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...

  6. netty04(重点来了、指定某个客户端发信息或者群发)小声嘀咕~~我也是从零开始学得、、、想学习netty的又不知道怎么下手的童鞋们~~

    还是和上几篇一样,先给出前面笔记的连接,有没看的可以去看看再来! netty01   . netty02  .netty03 看到这里.你基本上可以使用netty接受信息和根据对应的信息返回信息了 接 ...

  7. (转)openfire插件开发(三)通过http方式向openfire客户端发信息

    转:http://blog.csdn.net/hzaccp3/article/details/19964655 需求:  通过http方式,向openfire客户端发信息(非XMPP协议)openfi ...

  8. 网络编程(client发信息给server)

    client发信息给server

  9. 用python获取ip信息

    1.138网站 http://user.ip138.com/ip/首次注册后赠送1000次请求,API接口请求格式如下,必须要有token值 import httplib2 from urllib.p ...

随机推荐

  1. 单元测试系列之七:Sonar 数据库表关系整理一(rule相关)

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/7510072.html 简介:Sonar ...

  2. JDK下载-安装-配置

    1.JDK的下载 jdk1.8版本   下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...

  3. ckeditor5 增加居中alignment

    https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html 克隆下来 gi ...

  4. 00-python-常用命令

    1. pip 加速命令 pip install --index-url https://pypi.douban.com/simple 或者 pip install -i https://pypi.tu ...

  5. 揭示牌面使之升序 Reveal Cards In Increasing Order

    2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...

  6. 依赖注入demo

    让我们看一个例子: class UserProvider{ protected $connection; public function __construct(){ $this->connec ...

  7. 『Python』socket网络编程

    Python3网络编程 '''无论是str2bytes或者是bytes2str其编码方式都是utf-8 str( ,encoding='utf-8') bytes( ,encoding='utf-8' ...

  8. js中用户名的正则(字符,数字,下划线,减号)

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  9. springcloud-Eureka-服务注册与发现核心组件

    Eureka组件 Eureka是Netfilx开源的服务发现组件,本身是一个基于rest的服务,它包含client和server两部分. Spirng Cloud将它集成在子项目Spirng Clou ...

  10. sass command

    compass create --bare --sass-dir "sass" --css-dir "css" --javascripts-dir " ...