import requests import re from bs4 import BeautifulSoup def get_encoding(response): """获取页面编码""" encoding = response.apparent_encoding if encoding in ("ISO-8859-5", "ptcp154"): ret = re.search(r'charset=.*"', response.text) encoding = "".join(ret.group().split("=")[1][:-1]) if encoding in ["GB2312", "GBK"]: encoding = "GB18030" return encoding def check_server(): """ 河南交通事业发展中心主页,检查页面是否显示正常 :return: """ print(111) url = "http://www.hngl.cn/#/index" # headers = { # 'Accept': 'application/json, text/plain, */*', # "Accept-Encoding": "gzip, deflate", # "Accept-Language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6", # "Host": "hngl.cn", # "Origin": "http://www.hngl.cn", # "Referer": "http://www.hngl.cn/", # "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" # } response = requests.get(url) encoding = get_encoding(response) response.encoding = encoding text = response.content.decode(encoding) soup = BeautifulSoup(text, "lxml") # 获取信息公开栏数据 message_div = soup.find("div", attrs={"class": "xinxigongkai-list"}) one_text = message_div.find("a").text if one_text: print(one_text) else: print("服务异常")
import schedule import time from task.hnjt import check_server if __name__ == "__main__": check_server() # # 清空任务 # schedule.clear() # # # 每10分钟执行一次任务 # schedule.every(10).seconds.do(check_server) # # # 执行10S # while True: # schedule.run_pending() # time.sleep(1)
标签:__,服务,encoding,检测,server,text,import,response From: https://www.cnblogs.com/fuchenjie/p/17481126.html