分类目录归档:Python-Dev

[ Python二维码生成 ]


python二维码生成

  • flask版本
  • fastapi版本
  • 记得安装库
# from flask import Flask, make_response
# import qrcode
# import io

# app = Flask(__name__)

# @app.route("/image")
# def index():
#     qr = qrcode.QRCode(
#         version=3,
#         error_correction=qrcode.ERROR_CORRECT_L,
#         box_size=10

< Read more >

[ Selenium参考文章 ]


参考文章

  1. 安装需要先下载驱动
from selenium.webdriver.common.action_chains import ActionChains # 导包。鼠标
from selenium import webdriver # 浏览器导包
import time #时间导包
from selenium.webdriver.common.by import By # 元素导包
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expecte

< Read more >

[ 图片验证码识别 ]


git转换为图片(其实是由4张图片组成)

fron PIL inport Image
import numpy as np
import cv2

path = r'xxx.gif'
image = Image.open(path)

shapes = []
for i in range(0, 4):
    image.seek(i)
    image.save(r'./{}.png'.format(i))
    shapes.append(np.array(image))

cv2.imwrite(r'reult.png', '&

< Read more >

[ traceback模块 ]


异常日志记录

日志保存到文本

import traceback

a = ["hello", "yoyo"]
try:
    print(a[4])
except Exception as e:
    fp = open('log.txt', 'a')
    traceback.print_exc(file=fp)
    fp.close()

如果不想写入到文件,也可以暂时写入到内存(StringIO),后面需要用到的时候再读出来

from io import StringIO
import traceback

< Read more >

[ getopt函数使用 ]


import getopt

使用语法:getopt.getopt(args, options[, long_options])

参数说明:options是单字符,long_options表示字符串

返回结果:是一个list,每个list的项为(选项,值)的元组对

具体实例:#ping -c 12 -6 -s 1024 –help –size 1024

处理结果:

getopt.getopt(sys.argv[1:], ”c:s:6”, [“size=”,”help”])

[('-c', '12'), ('-6', ''), ('-s', '1024'), (“—help”,””), (“--s

< Read more >

[ @property配合@xx.setter ]


@*.setter

python中的@*.setter装饰器可以总结为两个作用:

对要存入的数据进行预处理
设置可读属性(不可修改)

注意:@.setter装饰器必须在@property装饰器的后面,且两个被修饰的函数的名称必须保持一致, 即为函数名称。

示例1

我们使用和上文一样的测试类,现在我们需要在数据存入user.age前先对其+5,利用@*.setter装饰器我们可以这样写:

class User():
    def __init__(self, name, age):
        self.name = name
        self._age = age

    

< Read more >