• 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()
  • 相关阅读:
    《CLR Via C# 第3版》笔记之(十四) 泛型高级
    《CLR Via C# 第3版》笔记之(十三) 泛型基础
    AOP学习基于Emit和Attribute的简单AOP实现
    《CLR Via C# 第3版》笔记之(十五) 接口
    C# 连接Oracle(利用ODP.net,不安装oracle客户端)
    《CLR Via C# 第3版》笔记之(十七) 线程基础
    C#直接读取磁盘文件(类似linux的Direct IO模式)
    《CLR Via C# 第3版》笔记之(十六) 字符串
    [置顶] C#中通过调用webService获取上网IP地址的区域的方法
    Android中Socket通讯类
  • 原文地址:https://www.cnblogs.com/liuzhongchao/p/8360662.html
Copyright © 2020-2023  润新知