求一个 使用python实现微信公众号发送天气预报的全部源代码

fasfj 2022-12-07 21:06:37

求一个 使用python实现微信公众号发送天气预报的全部源代码

...全文
91 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小袁同学. 2022-12-08
  • 打赏
  • 举报
回复

from time import localtime,time
import time as t
import random
import cityinfo
from requests import get, post
from datetime import datetime, date
import schedule

notes = ['世上之事,皆难如意,世间之人,都不如你。',
'没有电影电视剧的浪漫,不敢许你三生三世,只愿给你两厅三室。',
]

class config():
# 公众号配置
# 公众号appId
app_id = "公众号appId"
# 公众号appSecret
app_secret = "公众号appSecret"
# 模板消息id
template_id = "板消息id"
# 接收公众号消息的微信号
user = ["消息的微信号"]
# 称呼
name = '亲爱的'
# 信息配置
# 所在省份
province = "吉林"
# 所在城市
city = "松原"
# 在一起的日子,格式同上
love_date = "2019-7-21"

def get_access_token():
# appId
app_id = config.app_id
# appSecret
app_secret = config.app_secret
post_url = ("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}%22
.format(app_id, app_secret))
access_token = get(post_url).json()['access_token']
# print(access_token)
return access_token

def get_weather(province, city):
# 城市id
city_id = cityinfo.cityInfo[province][city]["AREAID"]
# city_id = 101280101
# 毫秒级时间戳
t = (int(round(time() * 1000)))
headers = {
"Referer": "http://www.weather.com.cn/weather1d/%7B%7D.shtml%22.format(city_id),
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
url = "http://d1.weather.com.cn/dingzhi/%7B%7D.html?_={}%22.format(city_id, t)
response = get(url, headers=headers)
response.encoding = "utf-8"
response_data = response.text.split(";")[0].split("=")[-1]
response_json = eval(response_data)
# print(response_json)
weatherinfo = response_json["weatherinfo"]
# 天气
weather = weatherinfo["weather"]
# 最高气温
temp = weatherinfo["temp"]
# 最低气温
tempn = weatherinfo["tempn"]
return weather, temp, tempn

def send_message(to_user, access_token, city_name, weather, max_temperature, min_temperature):
url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}%22.format(
access_token)
week_list = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
year = localtime().tm_year
month = localtime().tm_mon
day = localtime().tm_mday
today = datetime.date(datetime(year=year, month=month, day=day))
week = week_list[today.isoweekday()]
# 获取在一起的日子的日期格式
love_year = int(config.love_date.split("-")[0])
love_month = int(config.love_date.split("-")[1])
love_day = int(config.love_date.split("-")[2])
love_date = date(love_year, love_month, love_day)
# 获取在一起的日期差
love_days = str(today.sub(love_date)).split(" ")[0]
# 软文

notedetail = str(random.choice(notes))

data = {
    "touser": to_user,
    "template_id": config.template_id,
    "url": "http://weixin.qq.com/download",
    "topcolor": "#FF0000",
    "data": {
        "name": {
            "value": config.name,
            "color": "#9470dc"

        },
        "date": {
            "value": "{} {}".format(today, week),
            "color": "#00FFFF"
        },
        "note": {
            "value": notedetail,
            "color": "#FA8AA7"
        },

        "city": {
            "value": city_name,
            "color": "#808A87"
        },
        "weather": {
            "value": weather,
            "color": "#ED9121"
        },
        "min_temperature": {
            "value": min_temperature,
            "color": "#00FF00"
        },
        "max_temperature": {
            "value": max_temperature,
            "color": "#FF6100"
        },
        "love_day": {
            "value": love_days,
            "color": "#87CEEB"
        },
    }
}
headers = {
    'Content-Type': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
response = post(url, headers=headers, json=data)
print(response.text)
# print(data)

def start():
# 获取accessToken
accessToken = get_access_token()
# 接收的用户
# user = config.user
# 传入省份和市获取天气信息
province, city = config.province, config.city
weather, max_temperature, min_temperature = get_weather(province, city)

# 公众号推送消息
for user in config.user:
    send_message(user, accessToken, city, weather,
                 max_temperature, min_temperature)

schedule.every().day.at("7:00").do(start)

while True:

schedule.run_pending()

t.sleep(1)

start()

小袁同学. 2022-12-08
  • 打赏
  • 举报
回复

就是一个调用一个接口,然后公众号做个消息模版

芝麻粒儿 2022-12-07
  • 打赏
  • 举报
回复

Android-Unity一起学习啊

163

社区成员

发帖
与我相关
我的任务
社区描述
软件工程老师
python 高校 江苏省·南通市
社区管理员
  • juking@ntu
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧