| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- # -*- 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')
- import time
- import json
- from bs4 import BeautifulSoup
- import httpx
- from utils.utils_gotify import GotifyNotifier
- 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__":
- W = Weather().main()
|