You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.7 KiB
71 lines
2.7 KiB
# -*- coding: utf-8 -*-
|
|
'''
|
|
获取天气预报
|
|
'''
|
|
import os
|
|
import sys
|
|
import time
|
|
from datetime import datetime
|
|
from bs4 import BeautifulSoup
|
|
|
|
sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
|
|
|
|
from utils.utils_logs_handle import LogsHandle
|
|
from utils.utils_send_gotify import *
|
|
|
|
|
|
class Weather():
|
|
def __init__(self):
|
|
self.email_subject = '天气预报'
|
|
self.email_title = 'Weather forecast'
|
|
self.email_text = '获取数据时间:\n{0}\n{1}\n\n\n\n'.format(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
('-' * 90))
|
|
self.logs_handle = LogsHandle()
|
|
self.now_day = time.strftime('%Y-%m-%d', time.localtime())
|
|
|
|
def main(self):
|
|
self.logs_handle.logs_write('Weather forecast', '开始获取天气预报数据', 'start', False)
|
|
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) # 增加headers参数,简单伪装UA
|
|
response.encoding = "utf-8"
|
|
bs = BeautifulSoup(response.text, 'html.parser') # 这里我们用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)
|
|
self.logs_handle.logs_write('Weather forecast', e, 'error', False)
|
|
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()
|
|
|
|
self.logs_handle.logs_write('Weather forecast', '天气预报数据已获取', 'done', False)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
L = LogsHandle()
|
|
L.logs_write('Weather forecast', '开始获取天气预报数据', 'start', False)
|
|
W = Weather()
|
|
W.main()
|
|
L.logs_write('Weather forecast', '天气预报数据已获取', 'done', False)
|
|
|