weekfour——周总结
正则表达式
前戏
'''
案例:京东注册手机号校验
基本需求:手机号必须是11位、手机号必须以13 15开头、必须是纯数字
'''
phone = input('请输入您的手机号>>>:').strip()
if len(phone) == 11:
if phone.isdigit():
if phone.startswith('13') or phone.startswith('15'):
print('登录成功!')
else:
print('格式错误')
字符组
'''
字符组默认匹配方式是挨个挨个匹配
'''
[0123456789] # 匹配0到9任意一个数(全写)
[0-9] # 匹配0到9任意一个数(缩写)
[a-z] # 匹配26个小写英文字母
[A-Z] # 匹配26个大写英文字母
[0-9a-zA-Z] # 匹配数字或者小写字母或者大写字母
# ps:字符组内所有的数据默认都是或的关系
量词
'''
正则表达式默认情况下都是贪婪匹配>>>:尽可能多的匹配
'''
* # 匹配零次或多次 默认是多次(无穷次)
+ # 匹配一次或多次 默认是多次(无穷次)
? # 匹配零次或一次 作为量词意义不大主要用于非贪婪匹配
{n} # 重复n次
{n,} # 重复n次或更多次 默认是多次(无穷次)
{n,m} # 重复n到m次 默认是m次
# ps:量词必须结合表达式一起使用 不能单独出现 并且只影响左边第一个表达式
特殊符号
'''
特殊符号默认匹配方式是挨个挨个匹配
'''
. # 匹配除换行符以外的任意字符
\w # 匹配数字、字母、下划线
\W # 匹配非数字、非字母、非下划线
\d # 匹配数字
^ # 匹配字符串的开头
$ # 匹配字符串的结尾
#两者组合使用可以非常精确的限制匹配的内容
a|b # 匹配a或者b(管道符的意思是或)
() # 给正则表达式分组 不影响表达式的匹配功能
[] # 字符组 内部填写的内容默认都是或的关系
[^] # 取反操作 匹配除了字符组里面的其他所有字符
# 注意上尖号在中括号内和中括号意思完全不同
贪婪与非贪婪匹配
'''
所有的量词都是贪婪匹配如果想要变为非贪婪匹配只需要在量词后面加问号
'''
待匹配的文本
<script>alert(123)</script>
待使用的正则(贪婪匹配)
<.*>
请问匹配的内容
<script>alert(123)</script> 一条
# .*属于典型的贪婪匹配 使用它 结束条件一般在左右明确指定
待使用的正则(非贪婪匹配)
<.*?>
转义字符
'''
斜杠与字母的组合有时候有特殊含义
'''
\n 匹配的是换行符
\\n 匹配的是文本\n
\\\\n 匹配的是文本\\n
ps:如果是在python中使用 还可以在字符串前面加r取消转义
应用
'''
1.编写校验用户身份证号的正则
^[1-9]\d{13,16}[0-9x]$
^[1-9]\d{14}(\d{2}[0-9x])?$
^([1-9]\d{16}[0-9x]|[1-9]\d{14})$
2.编写校验邮箱的正则
3.编写校验用户手机号的正则(座机、移动)
4.编写校验用户qq号的正则
'''
re模块
'''
在python中如果想要使用正则 可以考虑re模块
'''
import re
# 常见操作方法
# res = re.findall('a', 'jason apple eva')
# print(res) # 查找所有符合正则表达式要求的数据 结果直接是一个列表
# res = re.finditer('a', 'jason apple eva')
# print(res) # 查找所有符合正则表达式要求的数据 结果直接是一个迭代器对象
# res = re.search('a', 'jason apple eva')
# print(res) # <re.Match object; span=(1, 2), match='a'>
# print(res.group()) # a 匹配到一个符合条件的数据就立刻结束
# res = re.match('a', 'jason apple eva')
# print(res) # None 匹配字符串的开头 如果不符合后面不用看了
# print(res.group()) # 匹配开头符合条件的数据 一个就结束
# obj = re.compile('\d{3}') # 当某一个正则表达式需要频繁使用的时候 我们可以做成模板
# res1 = obj.findall('23423422342342344')
# res2 = obj.findall('asjdkasjdk32423')
# print(res1, res2)
ret = re.split('[ab]', 'abcd') # 先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割
print(ret) # ['', '', 'cd']
ret = re.sub('\d', 'H', 'eva3jason4yuan4', 1) # 将数字替换成'H',参数1表示只替换1个
print(ret) # evaHjason4yuan4
ret = re.subn('\d', 'H', 'eva3jason4yuan4') # 将数字替换成'H',返回元组(替换的结果,替换了多少次)
print(ret) # ('evaHjasonHyuanH', 3)
re补充
'''
1.分组优先
'''
# res = re.findall('www.(baidu|oldboy).com', 'www.oldboy.com')
# print(res) # ['oldboy']
# findall分组优先展示:优先展示括号内正则表达式匹配到的内容
# res = re.findall('www.(?:baidu|oldboy).com', 'www.oldboy.com')
# print(res) # ['www.oldboy.com']
# res = re.search('www.(baidu|oldboy).com', 'www.oldboy.com')
# print(res.group()) # www.oldboy.com
# res = re.match('www.(baidu|oldboy).com', 'www.oldboy.com')
# print(res.group()) # www.oldboy.com
'''
2.分组别名
'''
res = re.search('www.(?P<content>baidu|oldboy)(?P<hei>.com)', 'www.oldboy.com')
print(res.group()) # www.oldboy.com
print(res.group('content')) # oldboy
print(res.group(0)) # www.oldboy.com
print(res.group(1)) # oldboy
print(res.group(2)) # .com
print(res.group('hei')) # .com
Python——爬虫准备工作
第三方模块的下载与使用
'''
第三方模块:
别人写的模块,一般情况下,功能都非常强大
使用第三方模块:
第一次使用必须先下载,后面才可以反复使用(下载后相当于内置模块)
下载第三方模块的方式:
pip是Python包管理工具,该工具提供了对Python包的查找、下载、安装和卸载的功能。
软件包也可以在https://pypi.org/ 中找到
目前最新版的python版本已经预装了pip
ps:每个版本的解释器都有自己的pip工具,在使用的时候,我们要注意自己用的到底是那个,为了避免pip冲突,我们可以在使用的时候添加对应的版本号。
下载第三方模块的相关操作:
下载第三方模块的dos命令:
pip install 模块名
下载第三方模块临时切换仓库:
pip install 模块名 -i 仓库地址
下载第三方模块指定版本(不指定为最新版)
pip install 模块名==版本号 -i 仓库地址
也可以使用Pycharm提供的快捷方式
下载第三方模块可能会出现的问题
1.报错并有警告信息
WARNING: You are using pip version 20.2.1;
原因在于pip版本过低 只需要拷贝后面的命令执行更新操作即可
d:\python38\python.exe -m pip install --upgrade pip
更新完成后再次执行下载第三方模块的命令即可
2.报错并含有Timeout关键字
说明当前计算机网络不稳定 只需要换网或者重新执行几次即可
3.报错并没有关键字
面向百度搜索
pip下载XXX报错:拷贝错误信息
通常都是需要用户提前准备好一些环境才可以顺利下载
4.下载速度很慢
pip默认下载的仓库地址是国外的 python.org
我们可以切换下载的地址
pip install 模块名 -i 仓库地址
pip的仓库地址有很多 百度查询即可
清华大学 :https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/
华中科技大学:http://pypi.hustunique.com/
豆瓣源:http://pypi.douban.com/simple/
腾讯源:http://mirrors.cloud.tencent.com/pypi/simple
华为镜像:https://repo.huaweicloud.com/repository/pypi/simple/
'''
requests模块
'''
Python中内置了requests模块,该模块只要用来发送HTTP请求。
requests模块能够模拟浏览器发送网络请求
'''
import requests
# 向指定网址发送请求,获取页面数据
res = requests.get('网址')
# content 获取bytes类型的网页数据
print(res.content)
# 指定编码
res.encoding = 'utf8'
# 获取字符串类型的网页数据(默认为utf8)
res.text
requests的属性和方法
方法 |
描述 |
delete(url, args) |
发送 delete 请求到指定 url |
get(url, params, args) |
发送 get 请求到指定 url |
head(url, data, args) |
发送 head 请求到指定 url |
patch(url, data, args) |
发送 patch 求到指定 url |
post(url, data, json, args) |
发送 post 请求到指定 url |
put(url, data, args) |
发送 put 请求到指定 url |
request(method, url, args) |
向指定的 url 发送指定的请求方法 |
爬取二手房数据
import requests
import re
res = requests.get('https://sh.lianjia.com/ershoufang/pudong/')
# print(res.text)
data = res.text
home_title_list = re.findall(
'<a class="" href=".*?" target="_blank" data-log_index=".*?" data-el="ershoufang" data-housecode=".*?" data-is_focus="" data-sl="">(.*?)</a>',
data)
# print(home_title_list)
home_name_list = re.findall('<a href=".*?" target="_blank" data-log_index=".*?" data-el="region">(.*?) </a>', data)
# print(home_name_list)
home_street_list = re.findall(
'<div class="positionInfo"><span class="positionIcon"></span><a href=".*?" target="_blank" data-log_index=".*?" data-el="region">.*? </a> - <a href=".*?" target="_blank">(.*?)</a> </div>',
data)
# print(home_street_list)
home_info_list = re.findall('<div class="houseInfo"><span class="houseIcon"></span>(.*?)</div>', data)
# print(home_info_list)
home_watch_list = re.findall('<div class="followInfo"><span class="starIcon"></span>(.*?)</div>', data)
# print(home_watch_list)
home_total_price_list = re.findall(
'<div class="totalPrice totalPrice2"><i> </i><span class="">(.*?)</span><i>万</i></div>', data)
# print(home_total_price_list)
home_unit_price_list = re.findall(
'<div class="unitPrice" data-hid=".*?" data-rid=".*?" data-price=".*?"><span>(.*?)</span></div>', data)
# print(home_unit_price_list)
home_data = zip(home_title_list, home_name_list, home_street_list, home_info_list, home_watch_list,
home_total_price_list, home_unit_price_list)
with open(r'home_data.txt','w',encoding='utf8') as f:
for data in home_data:
print(
"""
房屋标题:%s
小区名称:%s
街道名称:%s
详细信息:%s
关注程度:%s
房屋总价:%s
房屋单价:%s
"""%data
)
f.write("""
房屋标题:%s
小区名称:%s
街道名称:%s
详细信息:%s
关注程度:%s
房屋总价:%s
房屋单价:%s\n
"""%data)
自动化办公之openpyxl模块
'''
1.excel文件的后缀名问题
03版本之前
.xls
03版本之后
.xlsx
2.操作excel表格的第三方模块
xlwt往表格中写入数据、wlrd从表格中读取数据
兼容所有版本的excel文件
openpyxl最近几年比较火热的操作excel表格的模块
03版本之前的兼容性较差
ps:还有很多操作excel表格的模块 甚至涵盖了上述的模块>>>:pandas
3.openpyxl操作
'''
from openpyxl import Workbook
# 创建一个excel文件
wb = Workbook()
# 在一个excel文件内创建多个工作簿
wb1 = wb.create_sheet('学生名单')
wb2 = wb.create_sheet('舔狗名单')
wb3 = wb.create_sheet('海王名单')
# 还可以修改默认的工作簿位置
wb4 = wb.create_sheet('富婆名单', 0)
# 还可以二次修改工作簿名称
wb4.title = '高富帅名单'
wb4.sheet_properties.tabColor = "1072BA"
# 填写数据的方式1
# wb4['F4'] = 666
# 填写数据的方式2
# wb4.cell(row=3, column=1, value='jason')
# 填写数据的方式3
wb4.append(['编号', '姓名', '年龄', '爱好']) # 表头字段
wb4.append([1, 'jason', 18, 'read'])
wb4.append([2, 'kevin', 28, 'music'])
wb4.append([3, 'tony', 58, 'play'])
wb4.append([4, 'oscar', 38, 'ball'])
wb4.append([5, 'jerry', 'ball'])
wb4.append([6, 'tom', 88,'ball','哈哈哈'])
# 填写数学公式
# wb4.cell(row=1, column=1, value=12321)
# wb4.cell(row=2, column=1, value=3424)
# wb4.cell(row=3, column=1, value=23423432)
# wb4.cell(row=4, column=1, value=2332)
# wb4['A5'] = '=sum(A1:A4)'
# wb4.cell(row=8, column=3, value='=sum(A1:A4)')
# 保存该excel文件
wb.save(r'111.xlsx')
"""
openpyxl主要用于数据的写入 至于后续的表单操作它并不是很擅长 如果想做需要更高级的模块pandas
import pandas
data_dict = {
"公司名称": comp_title_list,
"公司地址": comp_address_list,
"公司邮编": comp_email_list,
"公司电话": comp_phone_list
}
# 将字典转换成pandas里面的DataFrame数据结构
df = pandas.DataFrame(data_dict)
# 直接保存成excel文件
df.to_excel(r'pd_comp_info.xlsx')
excel软件正常可以打开操作的数据集在10万左右 一旦数据集过大 软件操作几乎无效
需要使用代码操作>>>:pandas模块
"""
练习
# coding:utf-8
# 如何爬取二手房指定页数的数据
import re
import requests as requests
choice = input('请输入您想查看的页数').strip()
if not choice.isdigit():
print('请输入数字')
choice = int(choice)
for i in range(1, 101):
if choice in range(1, 101):
address = f'https://sh.lianjia.com/ershoufang/pudong/pg{choice}/'
else:
print('页数不存在')
print(address)
res = requests.get(address)
data = res.text
home_title_list = re.findall(
'<a class="" href=".*?" target="_blank" data-log_index=".*?" data-el="ershoufang" data-housecode=".*?" data-is_focus="" data-sl="">(.*?)</a>',
data)
home_name_list = re.findall('<a href=".*?" target="_blank" data-log_index=".*?" data-el="region">(.*?) </a>', data)
home_street_list = re.findall(
'<div class="positionInfo"><span class="positionIcon"></span><a href=".*?" target="_blank" data-log_index=".*?" data-el="region">.*? </a> - <a href=".*?" target="_blank">(.*?)</a> </div>',
data)
home_info_list = re.findall('<div class="houseInfo"><span class="houseIcon"></span>(.*?)</div>', data)
home_watch_list = re.findall('<div class="followInfo"><span class="starIcon"></span>(.*?)</div>', data)
home_total_price_list = re.findall(
'<div class="totalPrice totalPrice2"><i> </i><span class="">(.*?)</span><i>万</i></div>', data)
home_unit_price_list = re.findall(
'<div class="unitPrice" data-hid=".*?" data-rid=".*?" data-price=".*?"><span>(.*?)</span></div>', data)
home_data = zip(home_title_list, home_name_list, home_street_list, home_info_list, home_watch_list,
home_total_price_list, home_unit_price_list)
with open(r'home_data.txt', 'w', encoding='utf8') as f:
for data in home_data:
print(
"""
房屋标题:%s
小区名称:%s
街道名称:%s
详细信息:%s
关注程度:%s
房屋总价:%s万
房屋单价:%s
""" % data
)
f.write(
"""
房屋标题:%s
小区名称:%s
街道名称:%s
详细信息:%s
关注程度:%s
房屋总价:%s万
房屋单价:%s
""" % data
)
Python——模块补充及ATM分析
hashlib加密模块
一、加密相关概念
1.何为加密
将明文数据处理成密文数据 让人无法看懂
2.为什么加密
保证数据的安全
3.如何判断数据是否是加密的
一串没有规律的字符串(数字、字母、符号)
4.密文的长短有何讲究
密文越长表示使用的加密算法(数据的处理过程)越复杂
5.常见的加密算法有哪些
md5、base64、hmac、sha系列
二、加密算法基本操作
import hashlib
# 1.选择加密算法
md5 = hashlib.md5()
# 2.传入明文数据
md5.update(b'come de wei')
# 3.获取加密密文
res = md5.hexdigest()
print(res) # 71e83ce16391085a3f29fd2ee5cc2b6e
三、加密补充说明
1.加密算法不变 内容如果相同 那么结果肯定相同
md5 = hashlib.md5()
md5.update(b'come de wei A B C')
md5.update(b'come de wei ') # 一次性传可以
md5.update(b'A ') # 分多次传也可以
md5.update(b'B ') # 分多次传也可以
md5.update(b'C') # 分多次传也可以
2.加密之后的结果是无法反解密的
只能从明文到密文正向推导 无法从密文到明文反向推导
常见的解密过程其实是提前猜测了很多种结果
123 密文
321 密文
222 密文
3.加盐处理
在明文里面添加一些额外的干扰项
# 1.选择加密算法
md5 = hashlib.md5()
# 2.传入明文数据
md5.update('设置的干扰项'.encode('utf8'))
md5.update(b'hello python') # 一次性传可以
# 3.获取加密密文
res = md5.hexdigest()
print(res) # e53024684c9be1dd3f6114ecc8bbdddc
4.动态加盐
干扰项是随机变化的
eg:当前时间、用户名部分...
5.加密实战操作
1.用户密码加密
2.文件安全性校验
3.文件内容一致性校验
4.大文件内容加密
截取部分内容加密即可
subprocess模块
模拟操作系统终端 执行命令并获取结果
import subprocess
res = subprocess.Popen(
'asdas', # 操作系统要执行的命令
shell=True, # 固定配置
stdin=subprocess.PIPE, # 输入命令
stdout=subprocess.PIPE, # 输出结果
)
print('正确结果', res.stdout.read().decode('gbk')) # 获取操作系统执行命令之后的正确结果
print('错误结果', res.stderr) # 获取操作系统执行命令之后的错误结果
logging日志模块
1.如何理解日志
简单的理解为是记录行为举止的操作(历史史官)
2.日志的级别
五种级别
3.日志模块要求
代码无需掌握 但是得会CV并稍作修改
import logging
# logging.debug('debug message')
# logging.info('info message')
# logging.warning('warning message')
# logging.error('error message')
# logging.critical('critical message')
file_handler = logging.FileHandler(filename='x1.log', mode='a', encoding='utf8',)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
handlers=[file_handler,],
level=logging.ERROR
)
logging.error('你好')
日志的组成
1.产生日志
2.过滤日志
基本不用 因为在日志产生阶段就可以控制想要的日志内容
3.输出日志
4.日志格式
import logging
# 1.日志的产生(准备原材料) logger对象
logger = logging.getLogger('购物车记录')
# 2.日志的过滤(剔除不良品) filter对象>>>:可以忽略 不用使用
# 3.日志的产出(成品) handler对象
hd1 = logging.FileHandler('a1.log', encoding='utf-8') # 输出到文件中
hd2 = logging.FileHandler('a2.log', encoding='utf-8') # 输出到文件中
hd3 = logging.StreamHandler() # 输出到终端
# 4.日志的格式(包装) format对象
fm1 = logging.Formatter(
fmt='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
)
fm2 = logging.Formatter(
fmt='%(asctime)s - %(name)s: %(message)s',
datefmt='%Y-%m-%d',
)
# 5.给logger对象绑定handler对象
logger.addHandler(hd1)
logger.addHandler(hd2)
logger.addHandler(hd3)
# 6.给handler绑定formmate对象
hd1.setFormatter(fm1)
hd2.setFormatter(fm2)
hd3.setFormatter(fm1)
# 7.设置日志等级
logger.setLevel(10) # debug
# 8.记录日志
logger.debug('It's cool')
日志配置字典
import logging
import logging.config
# 定义日志输出格式 开始
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
'[%(levelname)s][%(message)s]' # 其中name为getlogger指定的名字
simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
# 自定义文件路径
logfile_path = 'a3.log'
LOGGING_DIC = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': standard_format
},
'simple': {
'format': simple_format
},
},
'filters': {}, # 过滤日志
'handlers': {
# 打印到终端的日志
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler', # 打印到屏幕
'formatter': 'simple'
},
# 打印到文件的日志,收集info及以上的日志
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler', # 保存到文件
'formatter': 'standard',
'filename': logfile_path, # 日志文件
'maxBytes': 1024 * 1024 * 5, # 日志大小 5M
'backupCount': 5,
'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了
},
},
'loggers': {
# logging.getLogger(__name__)拿到的logger配置
'': {
'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
'level': 'DEBUG',
'propagate': True, # 向上(更高level的logger)传递
}, # 当键不存在的情况下 (key设为空字符串)默认都会使用该k:v配置
# '购物车记录': {
# 'handlers': ['default','console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
# 'level': 'WARNING',
# 'propagate': True, # 向上(更高level的logger)传递
# }, # 当键不存在的情况下 (key设为空字符串)默认都会使用该k:v配置
},
}
logging.config.dictConfig(LOGGING_DIC) # 自动加载字典中的配置
# logger1 = logging.getLogger('购物车记录')
# logger1.warning('欢迎光临')
# logger1 = logging.getLogger('注册记录')
# logger1.debug('jason注册成功')
logger1 = logging.getLogger('天上人间顾客消费记录')
logger1.debug('Stronger')