pyhthon 利用爬虫结合阿里大于短信接口实现短信发送天气预报
Posted 若鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyhthon 利用爬虫结合阿里大于短信接口实现短信发送天气预报相关的知识,希望对你有一定的参考价值。
1 # -*- coding: utf-8 -*- 2 ‘‘‘‘‘ 3 SDK for alidayu 4 5 requires: python3.x, requests 6 7 @author: [email protected] 8 requests 打包出错故替换成urllib库 9 ‘‘‘ 10 11 #import requests 12 import urllib.request 13 import urllib.parse 14 import hashlib 15 from time import time 16 import json 17 import logging 18 19 logger = logging.getLogger(__name__) 20 21 22 class RestApi(object): 23 def __init__(self, key, secret, url="https://gw.api.tbsandbox.com/router/rest", partner_id=""): 24 self.key = key 25 self.secret = secret 26 self.url = url 27 self.partner_id = partner_id 28 29 def sign(self, params): 30 #=========================================================================== 31 # ‘‘‘签名方法 32 # @param parameters: 支持字典和string两种 33 # ‘‘‘ 34 #=========================================================================== 35 if isinstance(params, dict): 36 params = "".join(["".join([k, v]) for k,v in sorted(params.items())]) 37 params = "".join([self.secret, params, self.secret]) 38 sign = hashlib.md5(params.encode("utf-8")).hexdigest().upper() 39 return sign 40 41 def get_api_params(self): 42 params = {} 43 try: 44 [params.__setitem__(k, getattr(self, k)) for k in self.get_param_names()] 45 except AttributeError: 46 raise Exception("Some parameters is needed for this api call") 47 [params.__setitem__(k, getattr(self, k)) for k in self.get_option_names() if hasattr(self, k)] 48 print(params) 49 return params 50 51 def getResponse(self, authorize=None): 52 sys_params = { 53 "method": self.get_api_name(), 54 "app_key": self.key, 55 "timestamp": str(int(time() * 1000)), 56 "format": "json", 57 "v": "2.0", 58 "partner_id": self.partner_id, 59 "sign_method": "md5", 60 } 61 if authorize is not None: 62 sys_params[‘session‘] = authorize 63 params = self.get_api_params() 64 sign_params = sys_params.copy() 65 sign_params.update(params) 66 sys_params[‘sign‘] = self.sign(sign_params) 67 headers = { 68 ‘Content-type‘: ‘application/x-www-form-urlencoded;charset=UTF-8‘, 69 "Cache-Control": "no-cache", 70 "Connection": "Keep-Alive", 71 } 72 #headers = {"Content-Type":"application/x-www-form-urlencoded;charset=utf-8"} 73 74 sys_params.update(params) 75 logger.debug(json.dumps(sys_params)) 76 data = urllib.parse.urlencode(sys_params).encode(‘utf-8‘) 77 r = urllib.request.Request(self.url, data, headers) 78 result = urllib.request.urlopen(r).read().decode(‘utf-8‘) 79 return result 80 #r = requests.post(self.url, params=sys_params, headers=headers) 81 #r.raise_for_status() 82 #return r.json() 83 84 85 class AlibabaAliqinFcSmsNumSendRequest(RestApi): 86 def get_api_name(self): 87 return "alibaba.aliqin.fc.sms.num.send" 88 89 def get_param_names(self): 90 return [‘sms_type‘, ‘sms_free_sign_name‘, ‘rec_num‘, ‘sms_template_code‘] 91 92 def get_option_names(self): 93 return [‘extend‘, ‘sms_param‘]
调用接口
1 import json 2 from alidayu import AlibabaAliqinFcSmsNumSendRequest 3 from TianQi_Spider import GetWeather 4 5 6 7 def sms_send(phone): 8 weather = GetWeather() 9 10 appkey = ‘236******‘ 11 secret = ‘e81e81691c0cdbaacbfae2f***‘ 12 url = ‘https://eco.taobao.com/router/rest‘ 13 # params = {‘name‘:‘ks2‘,‘num‘:‘50‘} 14 req = AlibabaAliqinFcSmsNumSendRequest(appkey, secret, url) 15 req.extend = "123456" 16 req.sms_type = "normal" 17 req.sms_free_sign_name = "**天气" 18 #req.sms_param="{\"weather\":\"\"{name}.format(name=today_weather)\"\"}" 19 #req.sms_param=json.dumps(params) 20 req.sms_param=str(weather) 21 req.rec_num = phone 22 req.sms_template_code = "SMS_482***" 23 try: 24 resp = req.getResponse() 25 print(resp) 26 except Exception as e: 27 print(e) 28 29 30 31 sms_send(‘178***‘) 32 print("天气已爬取,短信发送成功")
以上是关于pyhthon 利用爬虫结合阿里大于短信接口实现短信发送天气预报的主要内容,如果未能解决你的问题,请参考以下文章