码迷,mamicode.com
首页 > 编程语言 > 详细

Python爬虫-播报天气信息(生成exe文件)待续

时间:2018-01-26 20:46:40      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:.net   title   target   else   获取   中国天气网   中国   gecko   集中   

#!/usr/bin/env python3
# -*- coding : utf-8 -*-

‘‘‘
1、从https://my.oschina.net/joanfen/blog/140364获取要播报城市code
2、采集中国天气网:http://www.weather.com.cn/weather/101120201.shtml
3、将采集到的文字百度语音合成为mp3文件,http://yuyin.baidu.com
‘‘‘
import time from bs4 import BeautifulSoup import requests import pymysql from aip import AipSpeech import os app_id = ***** api_key = ****** secret_key = ******* 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_url(city_code=101120201): url = http://www.weather.com.cn/weather/%s.shtml % city_code return url def get_data(url=http://www.weather.com.cn/weather/101120201.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: year = time.strftime(%Y,time.localtime(time.time())) month = time.strftime(%m, time.localtime(time.time())) day = time.strftime(%d, time.localtime(time.time())) text = year++month++day+日 %s天气预报,白天%s,最高温度%s,最低温度%s,%s,%s % (name, title, temperature_high, temperature_lower, win, win_lv) i += 1 return text def run(): city_name = 青岛 city_code = 101120201 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,per : 0 }) if not isinstance(result, dict): targetDir = D:\\python\\test\\spider\\static if not os.path.isdir(targetDir): os.mkdir(targetDir) with open(targetDir+\\weather_%s.mp3 % city_name, wb) as f: f.write(result) os.system(targetDir+\\weather_%s.mp3 % city_name) return city_name if __name__ == __main__: city = run()

 

Python爬虫-播报天气信息(生成exe文件)待续

标签:.net   title   target   else   获取   中国天气网   中国   gecko   集中   

原文地址:https://www.cnblogs.com/liuzhongchao/p/8360662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!