Python爬蟲-播報天氣資訊(產生exe檔案)待續

來源:互聯網
上載者:User

標籤:.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 timefrom bs4 import BeautifulSoupimport requestsimport pymysqlfrom aip import AipSpeechimport osapp_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 urldef 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 textdef 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_nameif __name__ == ‘__main__‘: city = run()

 

Python爬蟲-播報天氣資訊(產生exe檔案)待續

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.