Python3获取天气信息并通过百度语音播报

《Python3获取天气信息并通过百度语音播报》

最近学习Python,在学习的同时利用写了个获取天气的小爬虫,并通过百度语音来播放合成的天气信息。因为Python3对中文支持较好,所以爬虫使用Python3写。

天气的信息来源于新浪天气,使用lxml对信息筛选并获得自己想要的数据 。

将得到的信息与字符拼接即可得到一段包含关键信息的字符串,并将这段字符串与百度语音api连接到一起。

最后使用mplayer播放这段连接即可实现想要的效果。

最最后附上源码(还有点小问题不想改了,懒)。

#!/usr/bin/env Python
# coding = utf-8

import urllib.request
import re
import datetime
import traceback
from lxml import etree
import os

class programe(object):

	def get_content(self):
		try:
			url = 'http://weather.sina.com.cn/'
			header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
			request = urllib.request.Request(url, headers = header)
			response = urllib.request.urlopen(request)
			content = response.read().decode('utf-8')
			return content
		except:
			traceback.print_exc()

	#此处将数字转换成中文,因为纯数字百度语音播报不是很好
	def shuzi(self, number):		
		num = {'1':'一', '2':'二', '3':'三', '4':'四', '5':'五', '6':'六', '7':'七', '8':'八', '9':'九', '0':'〇'}
		bit = {1:'', 2:'十', 3:'百', 4:'千', 5:'万'}
		content, i = '', 1
		for s in number[::-1]:
			if i == 6:
				i = i - 4
			if s != '0':	
				content = num[s] + bit[i] + content
			i += 1
		return content

	def process(self, content):
		time = datetime.datetime.now().strftime('%H:%M')
		html = etree.HTML(content)
		address = html.xpath('//*[@id="slider_ct_name"]')[0].text					#获取所在地
		temperature_now = html.xpath('//*[@id="slider_w"]/div[1]/div/div[2]/div')[0].text 		#获取实时温度
		now = html.xpath('//*[@id="slider_w"]/div[1]/div/div[2]/p[2]')[0].text 				#获取信息并交给后面处理
		result = re.sub(' +|\xa0', ' ', now).strip().split(' ')
		weather_now = result[0]										#实时天气
		wind_now = result[5]										#实时风向及风力
		humidity_now = result[10][:-1]									#湿度
		pm = self.shuzi(html.xpath('//*[@id="blk_fc_c0_scroll"]/div[1]/ul/li[1]')[0].text) 		#PM2.5
		pm_status = html.xpath('//*[@id="blk_fc_c0_scroll"]/div[1]/ul/li[2]')[0].text			#PM2.5状态
		temperature = html.xpath('//*[@id="blk_fc_c0_scroll"]/div[1]/p[5]')[0].text 			#全天温度
		if time > '18:30':										#白天的页面和晚上有区别,下面对此做区分
			temperature_night = temperature
		else:
			temperature_day = re.split(' ', temperature)[0]
			temperature_night = re.split(' ', temperature)[2]
		wind = html.xpath('//*[@id="blk_fc_c0_scroll"]/div[1]/p[6]')[0].text 				#全天风向机风力
		Str_now = '现在时间:' + time + ',下面播报' + address + '天气,目前温度' + temperature_now + ',天气:' + weather_now + ',' + wind_now + ',' + humidity_now + '°,PM 2.5:' + pm + ',空气质量' + pm_status
		if time > '18:30':
			Str = '今天夜间最低温度' + temperature_night + ',全天' + wind + '.'
		else:
			Str = '今天白天最高温度' + temperature_day + ',夜间最低温度' + temperature_night + ',全天' + wind + '.'
		play = Str_now + ';' + Str
		#将处合成的信息使用百度语音api发送过去,并使用mplayer播放
		url = u'http://tts.baidu.com/text2audio?idx=1&tex={0}&cuid=baidu_speech_demo&cod=2&lan=zh&ctp=1&pdt=1&spd=4&per=12&vol=4&pit=5'.format(play)
		os.system('mplayer "%s"' % url)

	def main(self):
		content = self.get_content()
		self.process(content)


if __name__ == '__main__':
	print ('获取天气')
	print ('------------------------------')
	start = programe()
	start.main()

 

 

点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注