-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
116 lines (104 loc) · 3.76 KB
/
weather.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
# -*- coding: utf-8 -*-
# @Time : 2017/1/15 15:16
# @Author : woodenrobot
import os
import re
import time
import requests
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit'
'/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar'
'i/537.36',
}
def numtozh(num):
num_dict = {1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七',
8: '八', 9: '九', 0: '零'}
num = int(num)
if 100 <= num < 1000:
b_num = num // 100
s_num = (num-b_num*100) // 10
g_num = (num-b_num*100) % 10
if g_num == 0 and s_num == 0:
num = '%s百' % (num_dict[b_num])
elif s_num == 0:
num = '%s百%s%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, ''))
elif g_num == 0:
num = '%s百%s十' % (num_dict[b_num], num_dict.get(s_num, ''))
else:
num = '%s百%s十%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, ''))
elif 10 <= num < 100:
s_num = num // 10
g_num = (num-s_num*10) % 10
if g_num == 0:
g_num = ''
num = '%s十%s' % (num_dict[s_num], num_dict.get(g_num, ''))
elif 0 <= num < 10:
g_num = num
num = '%s' % (num_dict[g_num])
elif -10 < num < 0:
g_num = -num
num = '零下%s' % (num_dict[g_num])
elif -100 < num <= -10:
num = -num
s_num = num // 10
g_num = (num-s_num*10) % 10
if g_num == 0:
g_num = ''
num = '零下%s十%s' % (num_dict[s_num], num_dict.get(g_num, ''))
return num
def get_weather():
# 下载墨迹天气主页源码
res = requests.get('http://tianqi.moji.com/', headers=headers)
# 用BeautifulSoup获取所需信息
soup = BeautifulSoup(res.text, "html.parser")
temp = soup.find('div', attrs={'class': 'wea_weather clearfix'}).em.getText()
temp = numtozh(int(temp))
weather = soup.find('div', attrs={'class': 'wea_weather clearfix'}).b.getText()
sd = soup.find('div', attrs={'class': 'wea_about clearfix'}).span.getText()
sd_num = re.search(r'\d+', sd).group()
sd_num_zh = numtozh(int(sd_num))
sd = sd.replace(sd_num, sd_num_zh)
wind = soup.find('div', attrs={'class': 'wea_about clearfix'}).em.getText()
aqi = soup.find('div', attrs={'class': 'wea_alert clearfix'}).em.getText()
aqi_num = re.search(r'\d+', aqi).group()
aqi_num_zh = numtozh(int(aqi_num))
aqi = aqi.replace(aqi_num, aqi_num_zh)
info = soup.find('div', attrs={'class': 'wea_tips clearfix'}).em.getText()
sd = sd.replace(' ', '百分之').replace('%', '')
aqi = '空气质量' + aqi
# 获取今天的日期
today = datetime.now().date().strftime('%Y年%m月%d日')
# 将获取的信息拼接成一句话
text = '今天是%s,天气%s,温度%s摄氏度,%s,%s,%s,%s' % \
(today, weather, temp, sd, wind, aqi, info)
hour = datetime.now().hour
tmp = "早上"
if hour <= 11:
tmp = "早上"
elif hour > 11 and hour <= 13:
tmp = "中午"
elif hour > 13 and hour <= 18:
tmp = "下午"
else:
tmp = "晚上"
text = "主人" + tmp + "好," + text
return text
def text2voice(text):
os.system('python read.py "%s"' % text)
def main():
# 获取音乐文件绝对地址
mp3path2 = os.path.join(os.path.dirname(__file__), 'first.mp3')
# 先播放一首音乐做闹钟
os.system('mpg123 -q %s &' % mp3path2)
# 获取需要转换语音的文字
text = get_weather()
print(text)
# 播报语音天气
text2voice(text)
if __name__ == '__main__':
main()