| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- # -*- coding: utf-8 -*-
- '''
- 获取天气预报
- '''
- import os
- import sys
- sys.path.append(os.path.join(os.path.abspath(__file__).split('AutoInfo')[0] + 'AutoInfo'))
- SUB_PROJECT_NAME = "获取天气预报"
- PROJECT_PATH = os.path.join(os.path.abspath(__file__).split('AutoInfo')[0] + 'AutoInfo')
- from bs4 import BeautifulSoup
- from utils.utils import *
- class Weather():
- def main(self):
- print('开始获取天气预报数据')
- try:
- area_code = '59287'
- one_week = [
- '/tomorrow-%s.htm' % area_code,
- '/third-%s.htm' % area_code,
- '/fourth-%s.htm' % area_code,
- '/fifth-%s.htm' % area_code,
- '/sixth-%s.htm' % area_code,
- '/seventh-%s.htm' % area_code,
- ]
- url = "https://tianqi.2345.com/today-%s.htm" % area_code
- header = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8'}
- response = httpx.get(url=url, headers=header)
- response.encoding = "utf-8"
- bs = BeautifulSoup(response.text, 'html.parser')
- one_week_weather = []
- for week in one_week:
- a = bs.find_all('a', href=week)
- a = ' '.join(a[0].text.split())
- one_week_weather.append(a)
- except Exception as e:
- print(e)
- print('Weather forecast')
- exit(0)
- text = "天气预报: {}获取并发送\n".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) + '\n'.join(
- one_week_weather)
- # 推送到 message
- GotifyNotifier('天气预报数', text, 'weather').send_message()
- print('天气预报数据已获取')
- if __name__ == "__main__":
- try:
- W = Weather().main()
- except Exception as e:
- print(e)
- L = LogsHandle()
- L.logs_write('Weather forecast', str(e), 'error')
|