python定时发信息给女友
- 第一步,也是最难的一部
- 首先得要有个女朋友

- 利用python的第三方库wxpy来登录微信,实现消息发送功能
from wxpy import * def login():
bot = Bot(cache_path=True)
my_lover = bot.friends().search('夏叶')[0]
return my_loversearch方法接收一个昵称的字符串,它会返回一个查找到的所有条件的列表对象,我们这里只有这一个人,直接选第一个对象
- 去天气网爬取网页数据,将关于天气的信息筛选下来,整合成消息字符串
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定时发信息给女友的更多相关文章
- 利用python itchat给女朋友定时发信息
利用itchat给女朋友定时发信息 涉及到的技术有itchat,redis,mysql,最主要的还是mysql咯,当然咯,这么多东西,我就只介绍我代码需要用到的,其他的,如果需要了解的话,就需要看参考 ...
- Python定时框架 Apscheduler 详解【转】
内容来自网络: https://www.cnblogs.com/luxiaojun/p/6567132.html 在平常的工作中几乎有一半的功能模块都需要定时任务来推动,例如项目中有一个定时统计程序, ...
- 用python定时文章发布wordpress
用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...
- 新发现:AirDroid(用Web端控制自己的手机发信息)
http://web.airdroid.com/ 好多功能呀,有空研究研究 http://jingyan.baidu.com/article/b24f6c82cd4ade86bfe5daf3.html ...
- python定时利用QQ邮件发送天气预报
大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1.首先利用request库去请求数据,天气预报使用的是和风天气的API(www.heweather.c ...
- netty04(重点来了、指定某个客户端发信息或者群发)小声嘀咕~~我也是从零开始学得、、、想学习netty的又不知道怎么下手的童鞋们~~
还是和上几篇一样,先给出前面笔记的连接,有没看的可以去看看再来! netty01 . netty02 .netty03 看到这里.你基本上可以使用netty接受信息和根据对应的信息返回信息了 接 ...
- (转)openfire插件开发(三)通过http方式向openfire客户端发信息
转:http://blog.csdn.net/hzaccp3/article/details/19964655 需求: 通过http方式,向openfire客户端发信息(非XMPP协议)openfi ...
- 网络编程(client发信息给server)
client发信息给server
- 用python获取ip信息
1.138网站 http://user.ip138.com/ip/首次注册后赠送1000次请求,API接口请求格式如下,必须要有token值 import httplib2 from urllib.p ...
随机推荐
- i.MX6UL -- PWM用户空间使用方法【转】
本文转载自:https://blog.csdn.net/u014486599/article/details/53010114 i.MX6UL -- PWM用户空间使用方法 开发平台: 珠海鼎芯D51 ...
- vue2.0自学教程(一):走进vue2.0大观园
人狠话不多,咱直入主题吧! 开发软件: Visual Studio Code 支持环境: nodejs+windows 版本: vue2.0 1.初识vuevue是一个前端框架,使用MVVM(Mode ...
- webpack 与 vue 打包体积优化
webpack 与 vue 在使用vue开发时,遇到打包后单个文件太大,因而需要分包,不然加载时间太久.虽然尽可能减少请求次数,但是单个包太大也不是好事 思路 组件按需加载 vue-router 的懒 ...
- minicom 安装 查看串口
因为现在电脑基本不配备串行接口,所以,usb转串口成为硬件调试时的必然选择.目前知道的,PL2303的驱动是有的,在dev下的名称是ttyUSB0 默认情况下ubuntu已经安装了USB转串口驱动(p ...
- POJ 3278 抓奶牛(BFS入门题)
描述 农夫约翰已被告知逃亡牛的位置,并希望立即抓住她.他开始于一个点Ñ(0≤ Ñ ≤100,000)上的数线和牛是在点ķ(0≤ ķ上相同数目的线≤100,000).农夫约翰有两种交通方式:步行和传送. ...
- BluetoothGattCallback
/** * 用于实现 BluetoothGatt 的回调 */public abstract class BluetoothGattCallback { /** * GATT客户端连接或断开到远程的时 ...
- js里获取页面高度和文档高度
$(window).height() 和 $(document).height()的区别 $(window).height()代表了当前可见区域的大小,$(document).height()则代表了 ...
- 构建工具build tools
构建工具是从源代码自动创建可执行应用程序的程序(例如.apk for android app).构建包括将代码编译,链接和打包成可用或可执行的形式. 基本上,构建自动化是脚本或自动化软件开发人员在日常 ...
- YII框架实现 RBAC
(1).在 common\config\main.php添加 'components' => [ ’authManager’ => [ ’class’ => ...
- 结对作业——WordCount进阶版
Deadline: 2018-10-7 22:00PM,以博客提交至班级博客时间为准 要求参考来自:https://www.cnblogs.com/xinz/archive/2011/11/27/22 ...