python数据采集模块_Python 数据采集模块2

--coding:utf-8--

1.正则表达式(Regular Expression)

2.Re正则表达式模块

'''

python的内置模块,使用前 import re

表示方法:r'\d{3}\s+\d{3,8}'

2.2常用函数

re.complie(patern,flag = 0) :将字符串的正则表达式编译为Pattern对象

re.search(string[,pos[,endpos]]) 从string的任意位置开始匹配

re.match(string[,pos[,endpos]]) 从string的开头开始匹配 返回Match对象,否则返回None

re.findall(string[,pos[,endpos]]) 从string的任意位置开始匹配。返回一个列表

re.finditer(string[,pos[,endpos]]) 从string的任意位置开始匹配,返回一个迭代器

'''

import re

obj = re.match(r'\d{3}-\d{3,8}$','010-287688')

print(obj)

切分字符串 split()无法识别连续空格,使用re.split()+正则表达式组合

s = 'a b c d e r'

list01 = re.split(r'[\s,;]+',s)

list02 = s.split(' ')

print(list01)

print(list02)

3.logging日志模块

'''

python 内置标准库,用于输出运行日志,可以设置日志级别,日志保存路径,日志文件回滚

import logging

日志级别:使用范围,由高到低

1.FATAL:致命错误

2.CRITICAL:特别糟糕的,如内存耗尽,磁盘为空,一般很少用

3.ERROR:发生错误时,如IO操作失败或者连接问题

4.WARNING:发生很重要的时间,并不是错误

5.INFO:处理请求或者状态变化等日常事务

6.DEBUG:调试过程使用DEBUG等级

备注:5,6使用print()

'''

3.3使用

'''

1.设置日志首先需要使用logging.basiceConfig()函数设置日志信息的输出格式

基本语法:

logging.basicConfig(level = ?,format = ?)

level:日志级别

format:设置信息格式,如下

%(asctime)s:系统内时间

%(name)s:信息源名称

%(levelname)s:信息级别

%(message)s:信息内容

2.设置信息源名称,返回一个logger对象

logger = logging.getLogger(name)#name 系统级变量获取运行对象名称

'''

import logging

配置日志输出格式

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger(name)

logger.fatal('系统奔溃或发生致命错误,导致程序中断时需要输出的信息')

logger.critical('系统资源耗竭时需要输出的信息')

logger.error('系统报错异常时需要输出的信息')

logger.warning('系统运行警告时需要输出的信息')

logger.info('一般信息数据')

logger.debug('测试调试时需要输出的信息数据')

日志默认从info级别向上输出,debug不输出,太多

3日志写入文件

'''

FileHandle 用来将输出日志信息写入指定文件

步骤:

3.1 创建并设置logger

3.2 创建并设置FileHandle对象

3.3logger对象重新添加并替换原有的Handler处理器

'''

import logging

import os

import time

loger01 = logging.getLogger(name)

loger01.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

dirPath = os.path.join(os.getcwd(),'测试用例')

if not os.path.exists(dirPath):

os.mkdir(dirPath)

logFileName = time.strftime('%Y%m%d%H%M%S',time.localtime()) + 'log'

logPath = dirPath + os.sep + logFileName

fileHandle = logging.FileHandler(logPath)

fileHandle.setLevel(logging.INFO)

fileHandle.setFormatter(formatter)

loger01.addHandler(fileHandle)

loger01.fatal('系统奔溃或发生致命错误,导致程序中断时需要输出的信息')

loger01.critical('系统资源耗竭时需要输出的信息')

loger01.error('系统报错异常时需要输出的信息')

loger01.warning('系统运行警告时需要输出的信息')

loger01.info('一般信息数据')

loger01.debug('测试调试时需要输出的信息数据')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值