python获取天气接口给指定微信好友发天气预报


Posted in Python onDecember 28, 2020

先看下效果图:

python获取天气接口给指定微信好友发天气预报

用到的模块:

  • PyMySQL
  • requests
  • threading
  • wxpy

要实现上面的示例,首先是有两大块地方

  • 获取天气信息
  • 通过微信将天气信息发送出去

而获取天气信息又包括几个小的需要注意的地方

获取天气信息

  • 获取天气信息的接口
  • 获取天气信息的城市
  • 获取所在城市的城市码

假如我们给多个人发送天气情况,这几个人来自不同的城市,那么我们不可能每次都要输入城市名,然后查找城市码,然后再访问接口,获取天气情况,这样会非常的麻烦,所以我们需要考虑将城市名跟城市码一一对应起来,说到一一对应,首先想到的数据结构便是字典,所以我们可以将这些信息存入一个字典里,然后持久化到一个文件中,这样便方便很多

首先我们获取最新的 city 表,这个表是一个 list 类型,大体格式如下:

[
 {
  "id": 1,
  "pid": 0,
  "city_code": "101010100",
  "city_name": "北京",
  "post_code": "100000",
  "area_code": "010",
  "ctime": "2019-07-11 17:30:06"
 },
 {
  "id": 2,
  "pid": 0,
  "city_code": "",
  "city_name": "安徽",
  "post_code": null,
  "area_code": null,
  "ctime": null
 }
]

我们就简单的粘贴复制,放到一个空的列表中,如下所示,将所有的城市信息放到列表 citycode 中

citycode = [
 {
  "id": 1,
  "pid": 0,
  "city_code": "101010100",
  "city_name": "北京",
  "post_code": "100000",
  "area_code": "010",
  "ctime": "2019-07-11 17:30:06"
 },
...
...
...
...
...
...
 {
  "id": 2,
  "pid": 0,
  "city_code": "None",
  "city_name": "安徽",
  "post_code": "null",
  "area_code": "null",
  "ctime": "null"
 }
]

cityinfo = {}
#将城市名和城市代码写入json文件中
with open('city_for_code.json','w',encoding='utf-8') as f:
  for i in citycode:
    name = i["city_name"]
    code = i["city_code"]
    cityinfo[name] = code
  f.write(str(cityinfo))

#测试是否能读取
with open('city_for_code.json','r+',encoding='utf-8') as file:
  data_dst = file.readlines()
  d = eval(data_dst[0])

然后就是一顿处理,只把我们所需的 city_name 和 city_code 这俩字段取出即可,随后写入文件中。如果读取的话就按照上面方法去读取,需要注意的是,使用 open()方法读取文件,得到的内容是一个列表,我们需要通过 eval()方法转化成 dict 类型。

这是把 city_name 和 city_code 放到一个文件中的方法,另外我们也可以放到数据库中,这里以 MySQL 为例,安装 PyMySQL 模块

import pymysql

db_parames = {
  'host': 'localhost',
  'user': 'root',
  'password': '123456',
  'database': 'city_code_info'
}
#连接数据库
conn = pymysql.connect(**db_parames)

#创建游标对象,增删改查都在游标上进行
cursor = conn.cursor()

#表存在,就删除
cursor.execute("DROP TABLE IF EXISTS city_code")

#建表语句
create_table_sql = """CREATE TABLE `city_code` (
 `city_name` varchar(20) DEFAULT NULL,
 `city_code` varchar(25) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""
#建表
cursor.execute(create_table_sql)

#插入数据
with open('city_for_code.json','r+',encoding='utf-8') as f:
  origin_data = f.readlines()
  current_data = eval(origin_data[0])  #读取的内容是一个列表,且只包含一个元素
  #print(current_data.get('北京','Not Exists.'))
  for name, code in current_data.items():
    sql = """INSERT INTO city_code(city_name, city_code) VALUES ('%s', '%s')""" % (name, code)
    try:
      cursor.execute(sql)
    except:
      conn.rollback()
  conn.commit()
  conn.close()

执行这个 python 程序就可以将文件中的城市名跟城市码存到库中,当然我们也可以直接获取到城市名和城市码,然后跳过文件持久化这一步,直接把这两个字段取出存进去,但是考虑着代码要多练多写,就多此一举了一下。

下面是输入城市名就能得到城市码的代码块:

import pymysql

def get_city_code(city_name):
  db_parames = {
  'host': 'localhost',
  'user': 'root',
  'password': '123456',
  'database': 'city_code_info'
  }
  #连接数据库
  conn = pymysql.connect(**db_parames)

  #创建游标对象,增删改查都在游标上进行
  cursor = conn.cursor()

  #创建查询语句
  select_sql = "SELECT * FROM city_code where city_name='%s'"%(city_name)
  try:
    cursor.execute(select_sql)
    result = cursor.fetchall()
    for row in result:
      city_code = row[1]
    return city_code
  except:
    return "Error: unable fetch data!"

然后是根据输入的城市码来获取天气情况:

import requests

def get_weather(city_name,get_date_time=3):
  city_code = get_city_code(city_name)
  url = 'http://t.weather.sojson.com/api/weather/city/%s'%(city_code)
  header = {
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
  }
  response = requests.get(url,header)
  response.encoding = 'utf-8'
  weather = response.json()
  day = {1: '明天', 2: '后天', 3: '大后天'}
  weather_lst = []
  for num in range(get_date_time):
    City = weather["cityInfo"]["city"]
    Weatherganmao = weather["data"]["ganmao"]
    Weatherquality = weather["data"]["quality"]
    Weathershidu = weather["data"]["shidu"]
    Weatherwendu = weather["data"]["wendu"]
    Weatherpm25 = str(weather["data"]["pm25"])
    Weatherpm10 = str(weather["data"]["pm10"])
    Dateymd = weather["data"]["forecast"][num]["ymd"]
    Dateweek = weather["data"]["forecast"][num]["week"]
    Sunrise = weather["data"]["forecast"][num]["sunrise"]
    Sunset = weather["data"]["forecast"][num]["sunset"]
    Windfx = weather["data"]["forecast"][num]["fx"]
    Windf1 = weather["data"]["forecast"][num]["fl"]
    Weathertype = weather["data"]["forecast"][num]["type"]
    Weathernotice = weather["data"]["forecast"][num]["notice"]
    Weatherhigh = weather["data"]["forecast"][num]["high"]
    Weatherlow = weather["data"]["forecast"][num]["low"]
    if num == 0:
      result = '今日天气预报' + '\n' \
        + '日期: ' + Dateymd + ' ' + Dateweek + ' ' + City + '\n' \
        + '天气: ' + Weathertype + ' ' + Windfx + ' ' + Windf1 + ' ' + Weathernotice + '\n' \
        + '当前温度: ' + Weatherwendu + '℃' + '\n' \
        + '空气湿度: ' + Weathershidu + '\n' \
        + '温度范围: ' + Weatherlow + '' + '~' + '' + Weatherhigh + '\n' \
        + '污染指数: ' + 'PM2.5: ' + Weatherpm25 + ' ' + 'PM10: ' + Weatherpm10 + '\n' \
        + '空气质量: ' + Weatherquality + '\n' \
        + '日出时间: ' + Sunrise + '\n' \
        + '日落时间: ' + Sunset + '\n' \
        + '温馨提示: ' + Weatherganmao
    else:
      which_day = day.get(num,'超出范围')
      result = '\n' + which_day + ' ' + '天气预报' + '\n' \
        + '日期: ' + Dateymd + ' ' + Dateweek + ' ' + City + '\n' \
        + '天气: ' + Weathertype + ' ' + Windfx + ' ' + Windf1 + ' ' + Weathernotice + '\n' \
        + '温度范围: ' + Weatherlow + '' + '~' + '' + Weatherhigh + '\n' \
        + '日出时间: ' + Sunrise + '\n' \
        + '日落时间: ' + Sunset + '\n' \
        + '温馨提示: ' + Weatherganmao
    weather_lst.append(result)
    weather_str = ''   #因为默认要输出三天的天气情况,所以我们需要创建一个空字符串,然后每迭代一次,就将天气情况拼接到空字符串中。
    for msg in weather_lst:
      weather_str += msg + '\n'

  return weather_str

下面是发送微信消息

from wxpy import *

def send_wx(city_name, who):
  bot = Bot(cache_path=True)
  #bot = Bot(console_qr=2, cache_path='botoo.pkl')
  my_friend = bot.friends().search(who)[0]
  msg = get_weather(city_name)
  try:
    my_friend.send(msg)
  except:
    my_friend = bot.friends().search('fei')[0]
    my_friend.send(u"发送失败")

然后我们还需要写一个定时器,每隔一段时间便要发送一次

from threading import Timer

def auto_send():
  city_name = '设置要发送的城市'
  friend_list = ['要发送的人']

  for who in friend_list:
    send_wx(city_name,who)
  global timer
  timer = Timer(1,auto_send)
  timer.start()

最后执行程序

if __name__ == '__main__':
  timer = Timer(1,auto_send)
  timer.start()

以上就是python获取天气接口给指定微信好友发天气预报的详细内容,更多关于python获取天气接口的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python外星人入侵游戏编程完整版
Mar 30 Python
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
Feb 05 Python
Python callable()函数用法实例分析
Mar 17 Python
Python实现合并两个有序链表的方法示例
Jan 31 Python
python matplotlib库绘制散点图例题解析
Aug 10 Python
Laravel框架表单验证格式化输出的方法
Sep 25 Python
python实现小世界网络生成
Nov 21 Python
基于python读取.mat文件并取出信息
Dec 16 Python
Python基础之函数原理与应用实例详解
Jan 03 Python
Python随机数函数代码实例解析
Feb 09 Python
深入浅析Python 命令行模块 Click
Mar 11 Python
2021年pycharm的最新安装教程及基本使用图文详解
Apr 03 Python
详解python 条件语句和while循环的实例代码
Dec 28 #Python
一个非常简单好用的Python图形界面库(PysimpleGUI)
Dec 28 #Python
python函数超时自动退出的实操方法
Dec 28 #Python
Python 利用argparse模块实现脚本命令行参数解析
Dec 28 #Python
python中str内置函数用法总结
Dec 27 #Python
python中温度单位转换的实例方法
Dec 27 #Python
Python新建项目自动添加介绍和utf-8编码的方法
Dec 26 #Python
You might like
php通过前序遍历树实现无需递归的无限极分类
2015/07/10 PHP
可兼容php5与php7的cURL文件上传功能实例分析
2018/05/11 PHP
JavaScript 学习笔记(十六) js事件
2010/02/01 Javascript
js简单判断flash是否加载完成的方法
2016/06/21 Javascript
jQuery简单实现列表隐藏和显示效果示例
2016/09/12 Javascript
JavaScript函数节流和函数防抖之间的区别
2017/02/15 Javascript
vue2实现移动端上传、预览、压缩图片解决拍照旋转问题
2017/04/13 Javascript
linux 后台运行node服务指令方法
2018/05/23 Javascript
详解easyui基于 layui.laydate日期扩展组件
2018/07/18 Javascript
vue拖拽组件 vuedraggable API options实现盒子之间相互拖拽排序
2019/07/08 Javascript
javascript面向对象三大特征之封装实例详解
2019/07/24 Javascript
vue项目中使用eslint+prettier规范与检查代码的方法
2020/01/16 Javascript
Vuex的热更替如何实现
2020/06/05 Javascript
[01:39]2014DOTA2国际邀请赛 Newbee经理CU专访队伍火力全开
2014/07/15 DOTA
用Python中的wxPython实现最基本的浏览器功能
2015/04/14 Python
Java Web开发过程中登陆模块的验证码的实现方式总结
2016/05/25 Python
彻底理解Python list切片原理
2017/10/27 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
2018/01/29 Python
python 用下标截取字符串的实例
2018/12/25 Python
python里 super类的工作原理详解
2019/06/19 Python
Python 合并多个TXT文件并统计词频的实现
2019/08/23 Python
SpringBoot实现登录注册常见问题解决方案
2020/03/04 Python
keras的三种模型实现与区别说明
2020/07/03 Python
如何利用Python给自己的头像加一个小国旗(小月饼)
2020/10/02 Python
python 用opencv实现霍夫线变换
2020/11/27 Python
阿联酋手表和配饰购物网站:Rivolishop
2019/11/25 全球购物
掌上明珠Java程序员面试总结
2016/02/23 面试题
利用promise及参数解构封装ajax请求的方法
2021/03/24 Javascript
车辆维修工自我评价怎么写
2013/09/20 职场文书
党风廉正建设责任书
2015/01/29 职场文书
经费申请报告
2015/05/15 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
七年级作文之冬景
2019/11/07 职场文书
在HTML5 localStorage中存储对象的示例代码
2021/04/21 Javascript
我对PyTorch dataloader里的shuffle=True的理解
2021/05/20 Python
Python 语言实现六大查找算法
2021/06/30 Python