1.模块和包 # 模块的名字都是小写字母 # 一般都是把相关的功能放在一起 # 内置模块 # 第三方拓展模块 # 自定义模块 # 模块的导入:一般都是单行导入,遵循的是pep8规范 # # 一般是内置模块放最上面,第三方模块放下面,自定义模块放最后 # import haha # print(haha.a) # haha.func() # ret=haha.Yoyo() # print() # print(ret.name) # ret.gusu() # # # # # from haha import func # from haha import * # from haha import a,b,func # from haha import a as bac,b as cbd #取别名 # print(bac) # print(cbd) # 总结: # import 模块名 # from 模块名 import 变量名 # from 模块名 import 变量1,变量2 # from 模块名 import 变量1 as 新名字1,变量2 as 新名字2 # from 模块名 import * # 包的导入 # 包里会有__init__的py文件 from day13 import bao from day13 import bao as bie print(bao.a) print(bao.b) bao.func() print(bie.a)
2.正则表达式
# 正则表达式是什么?
# 前端,python,Java都有正则表达式
# 正则表达式只和字符串打交道、
# 主要功能:
# 1.从一大段文字中获取符合条件的内容(爬虫)
# 2.检测某一个字符串是否完全符合规则(表单验证)并不会真的去请求,它会判断你是否符合规则,符合才让发起请求
# 字符组
# 匹配所有的数字(0-9)
# 匹配所有的小写字母(a-z)
# 匹配所有的大写字母(A-Z)
# 匹配所有的字母和数字(a-zA-Z0-9) 简写(A-z0-9)
# 只能从ascii码的值值从小到大,一次可以取多个区间
# 元字符
# 匹配0-9所有的数字 d
# 匹配所有的字母和数字和下划线_ w
# 匹配所有的空白符 tab 空格 回车 s
# 匹配所有的非数字 D
# 匹配所有的非字母和数字和下划线_ w
# 匹配所有的非空白符 S
# 匹配非换行符(回车)以外的所有字符 .
# 匹配一个字符串的开始^
# 匹配一个字符串的结束$
# 只要不是abc都能匹配上[^abc]
# 以b结尾的b$
# 量词
# 表示匹配n次
# 1[3-9]d{9}匹配9次
#
# * 0-无穷大
# + 1-无穷大
# ? 表示0或1次 如果你写一个整数或小数 d+(.?d+)?
#
# {n}
# 表示匹配n次
# {n,m}表示匹配n-m次
#
# 贪婪匹配:尽量多的匹配
# (ab){1,3}
#
# 匹配任意的小数 d+.d+ 所有的小数需要前面加才是原本的意思
# 匹配任意的整数 [1-9]d*
# 总结:
# 1.只写元字符,相当于一个元字符表示一位字符上的内容
# 2.元字符加量词,量词只能约束前面的一个元字符 ab{3} 这个3只能约束这个b
# 3.(元字符元字符)量词 约束ab出现的次数 (ab){3}
3.re模块
import re
# findall匹配所有符合规则的,返回一个列表
# ret=re.findall('d+','hellokittly534543')
# print(ret)
# search 只返回从左到右第一个符合规则的
# ret2=re.search('d+','hellokittly534543,her324')
# print(ret2)
# print(ret2.group())
# renum='^1[3-9]d{9}'
# num=input('请输入手机号:')
# ret3=re.findall(renum,num)
# print(ret3)
#match 相当于默认在开头加了^号,从头开始匹配,如果开始匹配到了就成功,如果开始没匹配到就失败了,类似于search^
# ret4=re.match(r'd+','4234wind')
# print(ret4)
# print(ret4.group())
# split 切割
s='wind|666'
print(s.split('|'))
print(re.split(r'd+', 'wind134342'))
# sub 替换方法
print(re.sub(r'd+', 'H', '43wind134342',1))
# compile() 预编译 降低时间成本
rule=re.compile('d+')
print(rule.findall('43wind134342'))
# finditer 节省空间变成迭代器
ret=re.finditer('d', '43wind134342')
for i in ret: #迭代器取值
print(i.group())
4.sys模块
import sys
# sys.modules
# sys.path
# sys.argv
#
# print(sys.modules)#描述当前执行代码的位置,解释器中导入的所有模块都会被放到字典里
# 字典的key就是模块名字,对应value就是这个模块对应的内存中的位置
# import time
# print(time.time())
# print(sys.modules['time'].time())
# print(time==sys.modules['time'])
# print(sys.path)#路径列表,内置的模块都会存在这个路径列表里,从左到右依次去找
# 如果在sys.path中寻找数据的时候,能够找到一个文件,就不会继续往下走
import requests
content=requests.get('http://baidu.com')
print(content.content)
# sys.argv
# 通过argv可以从程序的外部进行输入,而非代码不呢神的地方,从外部来运行程序并给出参数
print(sys.argv)
if sys.argv[1]=='wind'and sys.argv[2]=='123':
print('登录成功,可以执行接下来的脚本功能')
else:
print('错误的用户名和密码')
datatime
from datetime import datetime
from datetime import timedelta
# now=datetime.now() #获取当前的时间
# print(now)
#
# ut=datetime.utcnow() #获取格林威治时间
# print(ut)
#
# print(now.day) #获取年月日
# print(now.month)
# print(now.year)
# print(now.hour)
# dt1=datetime(2022,6,7,15,22)
# dt2=datetime(2012,6,5,15,22)
# res=dt1-dt2 #计算时间差
# # print(type(dt1))
# print(res.day) #差多少天
# print(res.seconds) #差多少秒
# s1='2022/3/23'
# ret=datetime.strptime(s1,'%Y/%m/%d')
# print(ret,type(ret))
# s2='2022年10月30日Saturday' #这种转不了
# ret2=datetime.strptime(s2,'%Y/%m/%d')
# print(ret2)
# datetime 转 字符串
dt2=datetime(1997,8,25,10,20,9)
ret2=dt2.strftime('%Y/%m/%d %H:%M:%S')
print(ret2,type(ret2))
logging
import logging
# logging 日志分为5个等级
#
# logging.debug('debug message') #调试模式
# logging.info('info message') #基础信息
# logging.warning('warning message') #警告
# logging.error('error message') #错误
# logging.critical('critical message') #严重错误
#
# 设置日志的等级
# fh=logging.FileHandler(filename=r'test.log')
# sh=logging.StreamHandler()#在屏幕上输出
# logging.basicConfig(level=logging.DEBUG,handlers=[fh,sh ])
# logging.debug('debug message') #调试模式
# logging.info('info message') #基础信息
# logging.warning('warning message') #警告
# logging.error('error message') #错误
# logging.critical('critical message') #严重错误
# 优化日志
# sh=logging.StreamHandler()#在屏幕上输出
# file_handler=logging.FileHandler(filename='test1.log',encoding='utf-8',)
# logging.basicConfig(
# format='%(asctime)s - %(name)s[line:%(lineo)d] - %(levelname)s -%(module)s: %(message)s',
# datefmt='%Y-%m-%d %H:%M:%S %p',
# handlers=[file_handler,sh],
# level=logging.INFO
# )
# logging.error('你好')
# format='%(asctime)s #时间 %(filename)s #错误的文件 [line:%(lineo)d] #哪一行的错误 %(levelname)s #什么级别的错误 %(message)s',#错误信息
#
# 日志切割
import time
from logging import handlers
sh = logging.StreamHandler()
# rh = handlers.RotatingFileHandler('../log/test.log', maxBytes=1024, backupCount=5, encoding='utf-8') # 根据大小切
# maxBytes 最大的字节数超过就切, backupCount=5 最多备份5个,切掉第6个就删掉,把第6个命名第5个
fh = handlers.TimedRotatingFileHandler(filename='test2.log', when='s', interval=5, encoding='utf-8') #根据时间切
# interval=5 按5秒切一次, when='s' when='M' 按分钟 when='H' 按小时 when='D' 按天切 when='W' 按周切
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
handlers=[sh,fh],
level=logging.INFO
)
while True:
logging.info('111111111')
pymysql
import pymysql
conn = pymysql.connect(host='124.220.179.221', # 连接服务器的ip
user='root', # 用户名
password="123456", # 密码
database='iruance_cms', # 你想连接的数据库
port=3307, # 端口号
charset='utf8',
)
cursor=conn.cursor() # 获取游标
# cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) # 获取游标返回的是字典
sql="select * from sys_user where user_name='wind233'"
cursor.execute(sql)
print(cursor.fetchone()#一条一条取,如果没有返回NONE,节省内存
print(cursor.fetchmany(3))#指定取的条数,默认取一条
#插入 删除 修改 必须执行commit
sql="select * from sys_user where user_name='wind233'"
sql="update sys_user set user_name='wind789‘ where user_name='wind234'"
cursor.execute(sql)
conn.commit()#提交
# fetchall 取所有
print(cursor.fetchall()) #去所有
cursor.close() #关闭游标
conn.close() #关闭连接对象
# 字符串拼接
user_name='wind234'
sql="select count(1) from sys_user where user_name=%s"


