爬取天气网站获取天气信息,并生成语音播报
#!/usr/bin/env python3
# -*- coding : utf-8 -*-
'''从数据库中获取城市代码,城市code可从new_city_code.py中获取
即我另一篇文章城市code中
2017-8-9增加生成语音播报文件 参考yuyin.baidu.com
'''
from bs4 import BeautifulSoup
import requests
import pymysql
from aip import AipSpeech
import os
app_id = 'yours'
api_key = 'yours'
secret_key = 'yours'
db = pymysql.connect(host='localhost', user='dev', passwd='000000', db='test', use_unicode=True, charset='utf8')
cursor = db.cursor()
city_rul = 'https://my.oschina.net/joanfen/blog/140364'
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
def get_city_code(city_name):
sql = 'select city_code from weather_city_code where city_name= "%s"' % city_name
try:
cursor.execute(sql)
result = cursor.fetchone()
return result[0]
except:
print("抱歉,没有找到该城市~")
def get_url(city_code='101210101'):
url = 'http://www.weather.com.cn/weather/%s.shtml' % city_code
return url
def get_data(url='http://www.weather.com.cn/weather/101210101.shtml', name='杭州'):
html = requests.get(url)
html.encoding = 'utf-8'
bs = BeautifulSoup(html.text, 'lxml')
data = bs.find("div", {'id': '7d'})
ul = data.find('ul')
li = ul.find_all('li')
i = 0
for day in li:
weather = []
date = day.find('h1').string
wea = day.find_all('p')
title = wea[0].string
if wea[1].find('span') is None:
temperature_high = None
else:
temperature_high = wea[1].find('span').string
temperature_lower = wea[1].find("i").string
win = wea[2].find('span')['title']
win_lv = wea[2].find('i').string
weather.append(date)
weather.append(title)
weather.append(temperature_high)
weather.append(temperature_lower)
weather.append(win)
weather.append(win_lv)
print(weather)
if i == 0:
text = '你好,%s今天白天%s,最高温度%s,最低温度%s,%s,%s' % (name, title, temperature_high, temperature_lower, win, win_lv)
i += 1
return text
def run():
city_name = input('请输入你所在的城市名(ps:输入exit或quit退出程序):')
if city_name == 'exit' or city_name == 'quit':
db.close()
exit()
else:
city_code = get_city_code(city_name)
if city_code is None:
pass
else:
weather_url = get_url(city_code)
print('%s最近七日天气情况如下:' % city_name)
text = get_data(weather_url, city_name)
aipSpeech = AipSpeech(app_id, api_key, secret_key)
result = aipSpeech.synthesis(text, 'zh', 1, {
'vol': 5,
})
if not isinstance(result, dict):
with open('G:/python/MP3/weather_%s.mp3' % city_name, 'wb') as f:
f.write(result)
os.system('G:/python/MP3/weather_%s.mp3' % city_name)
return city_name
if __name__ == '__main__':
while True:
city = run()
这是打印
这是语音播报~