• 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()
  • 相关阅读:
    最短路径之迪杰斯特拉算法(Java)
    ipadmini从9.3.5降级8.4.1并完美越狱
    ubuntu中安装VNC供多用户访问(实验室运维踩坑)
    Linux在没有root权限的情况下安装gcc环境
    在WSL中运行32位程序
    将"在此处启动Windows Terminal"添加到右键菜单
    Windows中像在Linux里一样使用CMake和make
    ipadmini iOS8.4.1系统精简
    win10安装ubuntu子系统和xfce4.14图形界面
    FFT快速傅里叶变换
  • 原文地址:https://www.cnblogs.com/liuzhongchao/p/8360662.html
Copyright © 2020-2023  润新知