• python生成带参数二维码


    #coding:utf8
    import urllib2
    import urllib
    import json
    import string
    import random
    
    class WebChat(object):
    
        def __init__(self, appid=None, secret=None, code=None):
            self.appid = appid
            self.secret = secret
            self.code = code
    
        def token_url(self):
    
            return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" 
                   % (self.appid, self.secret)
    
        def get_access_token(self):
    
            access_token_url = self.token_url()
            f = urllib2.urlopen(access_token_url)
            json_string = f.read()
            return json.loads(json_string)['access_token']
    
        def get_ticket_url(self):
    
            access_token = self.get_access_token()
            url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s" %(access_token)
            return url
    
        def get_ticket(self):
        	body={
                    "action_name": "QR_LIMIT_SCENE", 
                    "action_info": {
                    "product_id":'d442kdD',
                    "scene": {
                            "scene_id": 1444
                            }
                                    }
                    }
            jsons = json.dumps(body)
            ticket_url = self.get_ticket_url()
            ret = urllib2.Request(ticket_url,jsons)
            result_string = urllib2.urlopen(ret).read()
            return json.loads(result_string)['ticket']
    
        def exchange_by_ticket(self):
            ticket = self.get_ticket()
            exchange_by_ticket_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s" % (ticket)
            return exchange_by_ticket_url
    
        def qrcode_name(self):
    	    allw = string.letters+string.digits
    	    r = []
    	    for i in range(6):
    	        r.append(random.choice(allw))
    	    return ''.join(r) 
    
        def get_qrcode_by_url(self):
        	url = self.exchange_by_ticket()
        	ret = urllib2.urlopen(url).read()
        	name = self.qrcode_name()
        	with open(name+'.jpg','wb') as f:
        		f.write(ret)
        		
        	return 1
    
    if __name__ =='__main__':
        web_chat = WebChat('XXXX','XXXX')
        result = web_chat.get_qrcode_by_url()
        print result
    

      

  • 相关阅读:
    无法往u盘里边复制超过4G的单个文件解决方法
    vue 自定义属性判断点击每个item 显示隐藏
    前端小组分享会之异步回调函数中的上下文
    git 的一些命令
    ES学习之promise
    mac找到占用端口号的进程ID
    meta link
    webpack学习笔记--安装
    css样式之 direction
    日常积累之JSON.stringify和JSON.parse substr
  • 原文地址:https://www.cnblogs.com/huangxiaohen/p/3858232.html
Copyright © 2020-2023  润新知