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

Posted 奔梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python爬虫-播报天气信息(生成exe文件)待续相关的知识,希望对你有一定的参考价值。

#!/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文件)待续的主要内容,如果未能解决你的问题,请参考以下文章

python爬虫抓取哈尔滨天气信息

企业级Python开发大佬利用网络爬虫技术实现自动发送天气预告邮件

企业级Python开发大佬利用网络爬虫技术实现自动发送天气预告邮件

利用Python网络爬虫采集天气网的实时信息—BeautifulSoup选择器

利用Python网络爬虫采集天气网的实时信息—BeautifulSoup选择器

Raspberry pi 定时天气播报