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 ...
随机推荐
- DDD之BoundedContext
原文 BoundedContext Bounded Context is a central pattern in Domain-Driven Design. It is the focus of D ...
- Learning-Python【15】:内置函数
截止到Python版本3.6.2,一共为我们提供了68个内置函数.它们就是Python提供的直接可以拿来使用的所有函数. 这个表的顺序是按照首字母的排列顺序来的,都混乱的堆在一起.比如,oct和bin ...
- FZOJ P2109 【卡德加的兔子】
题目描述 卡德加喜欢养兔子.他在达拉然的下水道里放了 $N$ 个兔笼(编号从 $1$ 到 $N$),里面养着他从德拉诺带来的兔子.它们的繁殖遵循斐波那契数列的规律:刚开始时,笼子里有一对刚出生的兔子. ...
- 移动Web UI库(H5框架)有哪些,看这里就够了
前言 今年上半年,项目组在项目开发的过程中建立了一套风格统一,组件丰富完善,命名统一规范的PC端UI库,适用于做大型站点,该UI库也是应用到了整个平台的项目中,在各个项目组中进行推广.因为项目的保密性 ...
- linux文件管理之链接文件
文件链接 ====================================================================================软链接 或 符号链接硬 ...
- linux存储管理之基本分区
基本分区管理 ====================================================================================基本分区(MBR| ...
- VSCode+python插件
1.打开VSCode 点击箭头所指地方 然后输入python 安装截图所示的插件 2.进行python路径设置 点击文件--首选项--设置 点击... 会弹出一个下拉框 选择打开setting.jso ...
- python模块--os模块
打印文件的绝对路径:os.path.abspath(__file__) os.path.dirname("/root/python/test.py") #只取目录名'/root ...
- MySQL造数据脚本-亲试
DELIMITER $$CREATE DEFINER=`root`@`192.168.2.254` PROCEDURE `pjzzspdz_fpmx_initdata12101245`()BEGIN ...
- Spring cloud系列之Zuul配置项中sensitiveHeaders和ignoredHeaders
sensitiveHeaders:会过滤客户端请求中的和该配置项匹配的headers比如: zuul: sensitiveHeaders: X-ABC 如果客户端在发请求是带了X-ABC,那么X-AB ...