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

    • 首先得要有个女朋友

    

  • 利用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. Spring Boot Log4j2 日志学习

    简介 Java 中比较常用的日志工具类,有: Log4j. SLF4j. Commons-logging(简称jcl). Logback. Log4j2(Log4j 升级版). Jdk Logging ...

  2. 论文笔记:Real-Time MDNet

    Real-Time MDNet ECCV 2018  2018-10-22 15:52:01 Paper:http://openaccess.thecvf.com/content_ECCV_2018/ ...

  3. TestNG详解(单元测试框架)

    一.TestNG的优点 1.1 漂亮的HTML格式测试报告 1.2 支持并发测试 1.3 参数化测试更简单 1.4 支持输出日志 1.5 支持更多功能的注解 二.编写TestNG测试用例的步骤 2.1 ...

  4. abstract class和interface的异同

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...

  5. 智能合约遇到的小错误 network up to date解决办法

    https://blog.csdn.net/qindong564950620/article/details/68933678 说 network up to date .这个错误我刚开始不知道怎么解 ...

  6. spring-cloud-config-server——Environment Repository(File System Backend)

    参考资料: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.0.RELEASE/single/spring-cl ...

  7. wrk 安装使用

    ==================== 安装 ====================https://github.com/wg/wrk/wiki sudo yum -y groupinstall ...

  8. 『Numpy』内存分析_numpy.dtype解析内存数据

    numpy.dtype用于自定义数据类型,实际是指导python程序存取内存数据时的解析方式. [注意],更改格式不能使用 array.dtype=int32 这样的硬性更改,会不改变内存直接该边解析 ...

  9. mongoose手动生成ObjectId

    用mongoose驱动保存数据,如果_id没有定义,那么在save的时候,mongoose驱动会自己生成一个_id.那么如果需要手动生成可以用mongoose.Types.ObjectId()方法. ...

  10. redis特性,使用场景

    redis特性: 1.redis保存在内存中,读写速度快. 2.redis--持久化(断电数据不丢失:对数据的更新将异步保存到磁盘上). 3.redis数据结构丰富 4.redis功能丰富 5.简单( ...