系统版本:
[root@nodchen-db01-test scripts]# cat /etc/redhat-release
CentOS release 6.9 (Final)
之前的python版本:
[root@nodchen-db01-test scripts]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
因为现在推广Python3 因而进行升级 保留原来的版本
1 #!/bin/bash 2 #install Mysql 编译安装 3 #author nod 4 #Date:18-07-08 5 #version 1.1 6 #Record: 7 8 #第一个里程碑/server/tools下 环境准备 9 cd /server/tools 10 tar xvf Python-3.6.3.tgz 11 12 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel -y 13 14 #第二个里程碑 安装 15 cd Python-3.6.3 16 17 ./configure --prefix=/usr/local/python3.6 18 19 make 20 make install 21 22 #第三个里程碑 创建软链接 23 24 ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python3 25 26 27 ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3
写好的Python代码
1 """ 2 Description: 3 需要提供以下三个信息,在申请到的微信企业号当中可以找到 4 agentid 5 corpid 6 corpsecret 7 Author:Nod 8 Date:18-04-10 9 Record: v1 1 先爬取当前天气 调用微信企业号进行发送 10 #---------------------------------v1-----------------------------------# 11 """ 12 13 from urllib.request import urlopen 14 from bs4 import BeautifulSoup 15 import re 16 import urllib.request 17 import json 18 import time 19 #-------------------------------- 20 # 获取当前时间 21 #-------------------------------- 22 time_format='%Y-%d-%m %X' 23 time_current=time.strftime(time_format) 24 25 #-------------------------------- 26 # 获取企业微信token 27 #-------------------------------- 28 29 def get_token(url, corpid, corpsecret): 30 token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret) 31 token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token'] 32 return token 33 34 #-------------------------------- 35 # 构建告警信息json 36 #-------------------------------- 37 def messages(msg): 38 values = { 39 "touser": '@all', 40 "msgtype": 'text', 41 "agentid": 1000002, #修改为对应应用的agentid 42 "text": {'content': msg}, 43 "safe": 0 44 } 45 msges=(bytes(json.dumps(values), 'utf-8')) 46 return msges 47 48 #-------------------------------- 49 # 发送告警信息 50 #-------------------------------- 51 def send_message(url,token, data): 52 send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,token) 53 respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read() 54 x = json.loads(respone.decode())['errcode'] 55 # print(x) 56 if x == 0: 57 print ('Succesfully') 58 else: 59 print ('Failed') 60 61 ##############函数结束######################## 62 63 corpid = 'ww7dd0074bd8b006f9' 64 corpsecret = '8gPCvguwomL0WMej8fcghxQgOY-y0LlGQsAicaDHvA8' 65 url = 'https://qyapi.weixin.qq.com' 66 67 #调取天气部分 68 69 resp=urlopen('http://www.weather.com.cn/weather/101191101.shtml') 70 soup=BeautifulSoup(resp,'html.parser') 71 tagToday=soup.find('p',class_="tem") #第一个包含class="tem"的p标签即为存放今天天气数据的标签 72 try: 73 temperatureHigh=tagToday.span.string #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。 74 except AttributeError as e: 75 temperatureHigh=tagToday.find_next('p',class_="tem").span.string #获取第二天的最高温度代替 76 77 temperatureLow=tagToday.i.string #获取最低温度 78 weather=soup.find('p',class_="wea").string #获取天气 79 80 # print('最低温度:' + temperatureLow) 81 # print('最高温度:' + temperatureHigh) 82 # print('天气:' + weather) 83 84 msg=time_current+' '+'常州天气汇总:'+'最低温度:' + temperatureLow+' 最高温度:' + temperatureHigh+' 天气:' + weather 85 86 #调取天气结束 87 88 #函数调用 89 test_token=get_token(url, corpid, corpsecret) 90 msg_data= messages(msg) 91 send_message(url,test_token, msg_data)
如果执行的有错误说明有的第三方依赖包么有安装
pip3 install bs4