日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python怎么发送微信给自己_Python新手教程:Python微信定时自动给【女朋友】发送天气预报...

發布時間:2024/7/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么发送微信给自己_Python新手教程:Python微信定时自动给【女朋友】发送天气预报... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果圖

主要思路

1.從wxpy獲取好友列表

2.創建定時器

3.定時器觸發函數

4.函數執行,遍歷好友列表

5.好友對象執行帶參函數,參數為該好友城市

6.函數中請求百度天氣接口,得到該好友對應天氣數據,解析處理數據,發送天氣信息,完成該對象發送。

7.遍歷結束,發送完畢

缺陷:打包為exe文件之后啟動失敗,原因是定時器找不到trigger,要想解決需要查看Apscheduler相關資料。

解決方法:換一種定時器。

編譯器上正常執行。

打包為exe之后,可以很方便發給別人使用。掃碼登錄后每天早上5:30會自動給所有好友發送效果圖中的天氣預報。

完整代碼

from wxpy import *

import requests

from datetime import datetime

import time

from apscheduler.schedulers.blocking import 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 = (' 早上好!這是今天的天氣預報!……機器人:PyChatBot\n')

results = result['results']

# 取出數據字典

data1 = results[0]

# 取出城市

city = data1['currentCity']

str1 =' 你的城市: %s\n' % city

# 取出pm2.5值

pm25 = data1['pm25']

str2 =' Pm值 : %s\n' % 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 =' 污染指數: %s\n' % pollution

result1 = results[0]

weather_data = result1['weather_data']

data = weather_data[0]

temperature_now = data['date']

str4 =' 當前溫度: %s\n' % temperature_now

wind = data['wind']

str5 =' 風向 : %s\n' % wind

weather = data['weather']

str6 =' 天氣 : %s\n' % weather

str7 =' 溫度 : %s\n' % data['temperature']

message = data1['index']

str8 =' 穿衣 : %s\n' % message[0]['des']

str9 =' 我很貼心: %s\n' % message[2]['des']

str10 =' 運動 : %s\n' % message[3]['des']

str11 =' 紫外線 : %s\n' % message[4]['des']

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

return str

Python資源分享qun 784758214 ,內有安裝包,PDF,學習視頻,這里是Python學習者的聚集地,零基礎,進階,都歡迎

好友列表

my_friends = []

my_friends = bot.friends()

my_friends.pop(0)

發送函數

def send_message():

給全體好友發送

for friend in 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()

總結

以上是生活随笔為你收集整理的python怎么发送微信给自己_Python新手教程:Python微信定时自动给【女朋友】发送天气预报...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。