效果图

from wxpyimport *

import requests

from datetimeimport datetime

import time

from apscheduler.schedulers.blockingimport BlockingScheduler#定时框架

bot = Bot(cache_path=True)

tuling = Tuling(api_key=你的api')#机器人api

def send_weather(location):

#准备url地址

path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'

url = path % location

response = requests.get(url)

result = response.json()

#如果城市错误就按照濮阳发送天气

if result['error'] !=0:

location ='濮阳'

url = path % location

response = requests.get(url)

result = response.json()

str0 = (' 早上好!这是今天的天气预报!……机器人:PyChatBotn')

results = result['results']

# 取出数据字典

data1 = results[0]

# 取出城市

city = data1['currentCity']

str1 =' 你的城市: %sn' % city

# 取出pm2.5值

pm25 = data1['pm25']

str2 =' Pm值 : %sn' % pm25

# 将字符串转换为整数 否则无法比较大小

if pm25 =='':

pm25 =0

pm25 =int(pm25)

# 通过pm2.5的值大小判断污染指数

if 0 <= pm25 <35:

pollution ='优'

elif 35 <= pm25 <75:

pollution ='良'

elif 75 <= pm25 <115:

pollution ='轻度污染'

elif 115 <= pm25 <150:

pollution ='中度污染'

elif 150 <= pm25 <250:

pollution ='重度污染'

elif pm25 >=250:

pollution ='严重污染'

str3 =' 污染指数: %sn' % pollution

result1 = results[0]

weather_data = result1['weather_data']

data = weather_data[0]

temperature_now = data['date']

str4 =' 当前温度: %sn' % temperature_now

wind = data['wind']

str5 =' 风向 : %sn' % wind

weather = data['weather']

str6 =' 天气 : %sn' % weather

str7 =' 温度 : %sn' % data['temperature']

message = data1['index']

str8 =' 穿衣 : %sn' % message[0]['des']

str9 =' 我很贴心: %sn' % message[2]['des']

str10 =' 运动 : %sn' % message[3]['des']

str11 =' 紫外线 : %sn' % message[4]['des']

str = str0 + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11

return str

#好友列表

my_friends = []

my_friends = bot.friends()

my_friends.pop(0)

#发送函数

def send_message():

#给全体好友发送

for friendin my_friends:

friend.send(send_weather(friend.city))

#发送成功通知我

bot.file_helper.send(send_weather('濮阳'))

bot.file_helper.send('发送完毕')

#定时器

print('star')

sched = BlockingScheduler()

sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=5,minute =30)

sched.start()

具体操作:

首先导入wxpy、图灵机器人和定时器Apscheduler,定时器用来定时群发。

具体pip操作建议百度。

使用百度的一个天气接口得到json数据。

主要思路:

1.从wxpy获取好友列表

2.创建定时器

3.定时器触发函数

4.函数执行,遍历好友列表

5.好友对象执行带参函数,参数为该好友城市

6.函数中请求百度天气接口,得到该好友对应天气数据,解析处理数据,发送天气信息,完成该对象发送。

7.遍历结束,发送完毕

缺陷:打包为exe文件之后启动失败,原因是定时器找不到trigger,要想解决需要查看Apscheduler相关资料。

解决方法:换一种定时器。

编译器上正常执行。

打包为exe之后,可以很方便发给别人使用。扫码登录后每天早上5:30会自动给所有好友发送效果图中的天气预报。

PS:下面看下Python实现微信定时发送天气预报

schedule实现定时

import requests

from requests import exceptions

from urllib.request import urlopen

from bs4 import BeautifulSoup

import re

from wxpy import *

import schedule

import time

bot=Bot(cache_path=True) #登陆网页微信,并保存登陆状态

def sendblogmsg(content):

#搜索自己的好友,注意中文字符前需要+u

my_friend = bot.friends().search(u'卿尘')[0]

my_friend.send(content)

#my_group = bot.groups().search(u'卿尘')[0]

#my_group.send(content) #发送天气预报

def job():

resp=urlopen('http://www.weather.com.cn/weather/101010100.shtml')

soup=BeautifulSoup(resp,'html.parser')

tagToday=soup.find('p',class_="tem") #第一个包含class="tem"的p标签即为存放今天天气数据的标签

try:

temperatureHigh=tagToday.span.string #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。

except AttributeError as e:

temperatureHigh=tagToday.find_next('p',class_="tem").span.string #获取第二天的最高温度代替

temperatureLow=tagToday.i.string #获取最低温度

weather=soup.find('p',class_="wea").string #获取天气

contents = '北京' + 'n' + '最高温度:' + temperatureHigh + 'n' + '最低温度:' + temperatureLow + 'n' + '天气:' + weather

# result3 = '最低温度:' + temperatureLow

#print('最低温度:' + temperatureLow)

#print('最高温度:' + temperatureHigh)

# print('天气:' + weather)

sendblogmsg(contents)

#定时

schedule.every().day.at("19:20").do(job) #规定每天12:30执行job()函数

while True:

schedule.run_pending()#确保schedule一直运行

time.sleep(1)

bot.join() #保证上述代码持续运行

end

总结

以上所述是小编给大家介绍的基于Python实现定时自动给微信好友发送天气预报,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

python发送文件给微信好友_基于Python实现定时自动给微信好友发送天气预报相关推荐

  1. 基于python的公众号课堂教学_基于Python的微信公众号数据挖掘分析

    基于Python的微信公众号数据挖掘分析 华南农业大学电子工程学院 王 建 黄宁香 [期刊名称]电子世界 [年(卷),期]2019(000)011 [总页数]3 运用Python网络爬虫技术对某时事类 ...

  2. python复制文件到另一个文件夹并重命名_基于python实现复制文件并重命名

    方法: shutil.copy("c://ccc//模板.xlsx","c://ccc//新文件.xlsx") 需求: 已知的Excel模板格式.已知的文件命名 ...

  3. python 微信视频_基于Python快速整理微信视频与图片

    photoes.jpg 作为超级APP,微信已经成了移动互联网的入口.而我们也早已习惯通过微信收发图片与视频等多媒体文件.身为一名屌丝通信狗,经常出差海外,跟家人的沟通方式主要就靠微信了,每当累了或抑 ...

  4. python中numpy数组的合并_基于Python中numpy数组的合并实例讲解

    基于Python中numpy数组的合并实例讲解 Python中numpy数组的合并有很多方法,如 - np.append() - np.concatenate() - np.stack() - np. ...

  5. 用python写脚本筛选原创微博_基于python编写的微博应用

    本文实例讲述了基于python编写的微博应用,分享给大家供大家参考.具体如下: 在编写自己的微博应用之前,先要到weibo开放平台申请应用的公钥和私钥. 下载python版的SDK,打开example ...

  6. python实现空气质量提醒程序_基于Python实现空气质量指数可视化

    前面我们已经爬取了全国城市空气质量数据( 基于Python实现城市空气质量爬取 ),基于之前我们爬取的数据,本文将使用Python将空气质量最好的前20个城市以柱状图的形式展示出来,点击对应的柱状图能 ...

  7. python怎样使用各个日期赤纬_基于Python的天文软件命令行界面设计与实现

    计算机软件技术的不断发展,推动了人机交互技术的长足进步.从传统的命令行(Command Line Interface,CLI),到图形用户界面(Graphical User Interface,GUI ...

  8. python版植物大战僵尸源码_基于python的植物大战僵尸游戏设计与实现.docx

    湖南理工学院毕业设计(论文) PAGE PAGE 1 学 号 毕业设计(论文) 题目:基于python的植物大战僵尸游戏设计与实现 作 者 届 别 届 院 别 信息与通信工程学院 专 业 信息工程 指 ...

  9. python入门文件读取与写入_使用Python对Dicom文件进行读取与写入的实现

    Pydicom 单张影像的读取 使用 pydicom.dcmread() 函数进行单张影像的读取,返回一个pydicom.dataset.FileDataset对象. import os import ...

最新文章

  1. Silverlight测试——利用Ranorex实现数据驱动测试
  2. C++ Error: no appropriate default constructor available
  3. java基础(九) 可变参数列表介绍
  4. windows 文件名太长无法删除的解决方法
  5. verilog学习记(测试和验证)
  6. Linux命令解释之rsync
  7. 关于用C#编写ActiveX控件3(转)
  8. idea主题下载导入工具
  9. DeepinXP_V5.8完美/增强精简版2合1
  10. 关于Si24r1调试的总结
  11. 制作u盘winpe启动盘_U教授工具如何制作U盘启动盘
  12. python输出10行带标号的hello、world_#000 Python 入门第一题通过扩展,学到了更多的知识...
  13. 曲线积分于曲面积分(后篇 曲面积分-坐标曲面积分-高斯公式-斯托克斯公式)
  14. python高阶知识之——列表推导式(63)
  15. 【模板】线段树 2 洛谷P3373
  16. arm鲲鹏服务器和x86区别
  17. python性能分析工具
  18. 小飞侠的游园方案答案
  19. tanner2019安装教程
  20. PCB画板软件那么多,我到底该学习哪一个?

热门文章

  1. 微信小程序----MUI数字输入框
  2. 重磅消息:谷歌300亿美元收购chatroulette旗下(我爱你 爱乐讯)520.ailexun.com 曲线重返中国
  3. 华为mate40e微信分身步骤分享
  4. 西加云杉700E设备AAA认证之移动设备的上下线操作
  5. 苹果6怎样打开html,苹果6屏幕旋转怎么设置 怎么开启和关闭【图文】
  6. http和https的区别https的连接方式
  7. 第一篇博客(图片路径在本地)
  8. ElementUI - <el-image> src 属性使用本地图片加载失败解决方案
  9. 套路:想戒手机?试试把屏幕变灰
  10. 被资本盯上的国产数据库