""" Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Date:18-06-13 Record: v1 1 先爬取当前天气 调用微信企业号进行发送 v2 : import urllib headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req = urllib.request.Request(url='http://www.weather.com.cn/weather/101191101.shtml', headers=headers) resp=urllib.request.urlopen(req).read() 应对爬虫处理方案:HTTP Error 403: Forbidden #---------------------------------v1-----------------------------------# """ from urllib.request import urlopen from bs4 import BeautifulSoup import re import urllib.request import json import urllib #-------------------------------- # 获取企业微信token #-------------------------------- def get_token(url, corpid, corpsecret): token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret) token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token'] return token #-------------------------------- # 构建告警信息json #-------------------------------- def messages(msg): values = { "touser": '@all', "msgtype": 'text', "agentid": 1000002, #修改为对应应用的agentid "text": {'content': msg}, "safe": 0 } msges=(bytes(json.dumps(values), 'utf-8')) return msges #-------------------------------- # 发送告警信息 #-------------------------------- def send_message(url,token, data): send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,token) respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read() x = json.loads(respone.decode())['errcode'] # print(x) if x == 0: print ('Succesfully') else: print ('Failed') ##############函数结束######################## corpid = 'ww7dd0074bd8b006f9' corpsecret = '8gPCvguwomL0WMej8fcghxQgOY-y0LlGQsAicaDHvA8' url = 'https://qyapi.weixin.qq.com' #调取天气部分 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req = urllib.request.Request(url='http://www.weather.com.cn/weather/101191101.shtml', headers=headers) resp=urllib.request.urlopen(req).read() soup=BeautifulSoup(resp,'html.parser') tagToday=soup.find('p',class_="tem") #第一个包含class="tem"的p标签即为存放今天天气数据的标签 try: temperatureHigh=tagToday.span.string #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。 except AttributeError as e: temperatureHigh=tagToday.find_next('p',class_="tem").span.string #获取第二天的最高温度代替 temperatureLow=tagToday.i.string #获取最低温度 weather=soup.find('p',class_="wea").string #获取天气 # print('最低温度:' + temperatureLow) # print('最高温度:' + temperatureHigh) # print('天气:' + weather) msg='常州天气汇总:'+'最低温度:' + temperatureLow+' 最高温度:' + temperatureHigh+' 天气:' + weather #调取天气结束 #函数调用 test_token=get_token(url, corpid, corpsecret) msg_data= messages(msg) send_message(url,test_token, msg_data)
urllib.request.urlopen() 方法经常会被用来打开一个网页的源代码,然后会去分析这个页面源代码,但是对于有的网站使用这种方法时会抛出"HTTP Error 403: Forbidden"异常
因而对程式进行了更新
对比老版本:
下面运行目前会有报错
""" Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Date:18-04-10 Record: v1 1 先爬取当前天气 调用微信企业号进行发送 #---------------------------------v1-----------------------------------# """ from urllib.request import urlopen from bs4 import BeautifulSoup import re import urllib.request import json #-------------------------------- # 获取企业微信token #-------------------------------- def get_token(url, corpid, corpsecret): token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret) token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token'] return token #-------------------------------- # 构建告警信息json #-------------------------------- def messages(msg): values = { "touser": '@all', "msgtype": 'text', "agentid": 1000002, #修改为对应应用的agentid "text": {'content': msg}, "safe": 0 } msges=(bytes(json.dumps(values), 'utf-8')) return msges #-------------------------------- # 发送告警信息 #-------------------------------- def send_message(url,token, data): send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,token) respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read() x = json.loads(respone.decode())['errcode'] # print(x) if x == 0: print ('Succesfully') else: print ('Failed') ##############函数结束######################## corpid = 'ww7dd0074bd8b006f9' corpsecret = '8gPCvguwomL0WMej8fcghxQgOY-y0LlGQsAicaDHvA8' url = 'https://qyapi.weixin.qq.com' #调取天气部分 resp=urlopen('http://www.weather.com.cn/weather/101191101.shtml') soup=BeautifulSoup(resp,'html.parser') tagToday=soup.find('p',class_="tem") #第一个包含class="tem"的p标签即为存放今天天气数据的标签 try: temperatureHigh=tagToday.span.string #有时候这个最高温度是不显示的,此时利用第二天的最高温度代替。 except AttributeError as e: temperatureHigh=tagToday.find_next('p',class_="tem").span.string #获取第二天的最高温度代替 temperatureLow=tagToday.i.string #获取最低温度 weather=soup.find('p',class_="wea").string #获取天气 # print('最低温度:' + temperatureLow) # print('最高温度:' + temperatureHigh) # print('天气:' + weather) msg='常州天气汇总:'+'最低温度:' + temperatureLow+' 最高温度:' + temperatureHigh+' 天气:' + weather #调取天气结束 #函数调用 test_token=get_token(url, corpid, corpsecret) msg_data= messages(msg) send_message(url,test_token, msg_data)