forked from tech-shrimp/GithubActionSample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_report.py
125 lines (107 loc) · 4.53 KB
/
weather_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 安装依赖 pip3 install requests html5lib bs4 schedule
import os
import requests
import json
from bs4 import BeautifulSoup
# 从测试号信息获取
appID = os.environ.get("APP_ID")
appSecret = os.environ.get("APP_SECRET")
# 收信人ID即 用户列表中的微信号
openId = os.environ.get("OPEN_ID")
# 天气预报模板ID
weather_template_id = os.environ.get("TEMPLATE_ID")
def get_weather(my_city):
url = "http://www.weather.com.cn/textFC/guangdong.shtml"
resp = requests.get(url)
text = resp.content.decode("utf-8")
soup = BeautifulSoup(text, 'html5lib')
div_conMidtab = soup.find("div", class_="conMidtab3")
tables = div_conMidtab.find_all("table")
weather_info = None
for table_index, table in enumerate(tables):
print(f"Table {table_index}:")
trs = table.find_all("tr")
if len(trs) > 0:
for tr_index, tr in enumerate(trs):
print(f" Row {tr_index}:")
tds = tr.find_all("td")
if len(tds) < 9: # 跳过不完整的数据行
continue
# 获取区县名
city_td = tds[0] if 'rowsPan' in tds[0].get('class', []) else tds[1]
this_city = list(city_td.stripped_strings)[0]
print(f" 当前城市: {this_city}") # 调试信息
if this_city == my_city:
# 获取白天天气和夜间天气信息
weather_day = list(tds[2].stripped_strings())[0]
wind_day = list(tds[3].stripped_strings())[0] + list(tds[3].find('span', class_='conMidtabright').stripped_strings())[0]
high_temp = list(tds[4].stripped_strings())[0]
weather_night = list(tds[5].stripped_strings())[0]
wind_night = list(tds[6].stripped_strings())[0] + list(tds[6].find('span', class_='conMidtabright').stripped_strings())[0]
low_temp = list(tds[7].stripped_strings())[0]
temp = f"{low_temp}——{high_temp}摄氏度" if high_temp != "-" else f"{low_temp}摄氏度"
weather_typ = weather_day if weather_day != "-" else weather_night
wind = f"{wind_day}" if wind_day != "--" else f"{wind_night}"
print(f"天气信息: {this_city}, {temp}, {weather_typ}, {wind}") # 调试信息
weather_info = (this_city, temp, weather_typ, wind)
break # 找到目标城市后跳出内层循环,但继续检查下一个table
if not weather_info:
print(f"未找到城市: {my_city}") # 调试信息
return None
return weather_info
def get_access_token():
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(appID.strip(), appSecret.strip())
response = requests.get(url).json()
access_token = response.get('access_token')
return access_token
def get_daily_love():
url = "https://api.lovelive.tools/api/SweetNothings/Serialization/Json"
r = requests.get(url)
all_dict = json.loads(r.text)
sentence = all_dict['returnObj'][0]
daily_love = sentence
return daily_love
def send_weather(access_token, weather):
if weather is None:
print("未能获取天气信息,不发送通知。")
return
if "雨" not in weather[2]:
print(f"天气不是雨天,不发送通知。天气信息:{weather[2]}")
return
import datetime
today = datetime.date.today()
today_str = today.strftime("%Y年%m月%d日")
body = {
"touser": openId.strip(),
"template_id": weather_template_id.strip(),
"url": "https://weixin.qq.com",
"data": {
"date": {
"value": today_str
},
"region": {
"value": weather[0]
},
"weather": {
"value": weather[2]
},
"temp": {
"value": weather[1]
},
"wind_dir": {
"value": weather[3]
},
"today_note": {
"value": get_daily_love()
}
}
}
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)
print(requests.post(url, json.dumps(body)).text)
def weather_report(this_city):
access_token = get_access_token()
weather = get_weather(this_city)
print(f"天气信息: {weather}")
send_weather(access_token, weather)
if __name__ == '__main__':
weather_report("宝安")