背景
- 目的:通过百度搜索指定关键词,然后抓取前三页网页的广告
技术栈小结
time
import time
now = lambda : time.perf_counter()
# 暂停5s
time.sleep(5)
>>> time.time() # 时间戳
1613220602.8661115
>>> time.localtime() # 本地时间
time.struct_time(tm_year=2021, tm_mon=2, tm_mday=13, tm_hour=20, tm_min=49, tm_sec=57, tm_wday=5, tm_yday=44, tm_isdst=0)
>>> time.asctime(time.localtime()) # 获取格式化时间
'Sat Feb 13 20:35:31 2021'
>>> time.asctime(time.localtime(time.time()))
'Sat Feb 13 20:58:19 2021'
>>> time.strftime('%Y%m%d', time.localtime()) # 格式化时间
'20210213'
>>>
os
# 文件/目录方法
>>> import os
>>> os.getcwd() # 当前工作文件夹
>>> os.chdir(path) # 修改当前工作文件目录为path
>>> os.listdir(path) # 返加指定路径下path中的文件夹及文件名称
>>> os.mkdir(path) # 新建文件夹
>>> os.rmdir(path) # 删除空文件夹 -- 文件夹非空 OSError
>>> os.remove(path) # 删除文件
>>> os.rename(oldName, newName) # 修改文件名
>>> os.stat() # 获取指定路径的信息
# os.stat()
# st_atime 上次访问时间;st_mtime 最近修改时间; st_ctime 创建时间
>>> os.stat(os.getcwd())
os.stat_result(st_mode=16749, st_ino=1688849860301781, st_dev=3370681046, st_nlink=1, st_uid=0, st_gid=0, st_size=61440, st_atime=1613227168, st_mtime=1613227168, st_ctime=1523514771)
>>> os.stat(os.getcwd()).st_ctime
1523514771.5762281
os.path模块
import os
# 获取文件的属性信息
os.path.abspath(path) # 返回绝对路径
os.path.dirname(path) # 返回文件路径
os.path.basename(path) # 返回文件名
os.path.exists(path) # 判定文件路径是否存在,是True,否False
os.path.expanduser('~') # 返回用户目录
>>> os.path.expanduser('~tt') # 将用户目录换为用户(tt)目录
'C:\\Users\\tt'
>>> os.path.getmtime(r't.py') # 返回最近修改时间
1612868196.803879
>>> os.path.getsize(r't.py') # 返回文件大小
1862
os.path.isabs(path) # 判定是否为绝对路径
os.path.isfile(path) # 判定是否文件
os.path.isdir(path) # 判定是否为目录
>>> os.path.join(os.getcwd(),'t', 'a','c') # 将目录、文件夹、文件合并为路径
'c:\\users\\chen.huaiyu\\desktop\\t\\a\\c'
>>> os.path.split(os.path.join(os.path.expanduser('~tt'),r't.py')) # 将路径分割为dirname & basename,返回元组
('C:\\Users\\tt', 't.py')
>>> os.path.splitext(os.path.join(os.path.expanduser('~tt'),r't.py')) # 分割为路径名 & 文件拓展名
('C:\\Users\\tt\\t', '.py')
>>> os.path.splitext(r't.py')
('t', '.py')
logging
-
logging模块的日志级别:1 -> 5依次升高。
1.DEBUG 问题诊断;
2.INFO 关键节点信息,检查程序是否按预期运行;
3 WARNING 不期望的事情发生;
4.ERROR 严重的问题发生,导致某些功能不能正常使用;
5.CRITICAL 严重错误,导致程序无法正常动行。 -
logging四大组件:
1.Loggers,提供日志使用接口;
2.Handles,将日志发送到指定位置;
3.Filters,过滤日志,决定哪些日志将会被记录;
4.Formatters,控制日志输出格式 -
logging模块的使用方式:
1.使用logging提供的模块级别的函数;
# logging.info(msg, *args)
logging.info('%s is %d years old', 'Tom', 10)
# exc_info:True - 将异常信息添加到日志中
# stack_info:默认False,True - 栈信息将被添加到日志中
# extra: dict参数,自定义消息格式中的字段
>>> import logging
>>> LOG_FORMAT = '%(asctime)s - %(levelname)s - %(user)s[%(ip)s] - %(message)s'
>>> DATE_FORMAT = '%m/%d/%Y %H:%M:%S %p'
>>> logging.basicConfig(format=LOG_FORMAT, datefmt=DATE_FORMAT)
>>> logging.warning('Some one delete the log file.', exc_info=True, stack_info=True, extra={
'user':'Tom', 'ip':'10.10.10.10'})
02/14/2021 21:11:43 PM - WARNING - Tom[10.10.10.10] - Some one delete the log file.
NoneType: None
Stack (most recent call last):
File "<string>", line 1, in <module>
File "D:\...\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "D:\...\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "<pyshell#70>", line 1, in <module>
2.使用Logging日志系统的四大组件:日志器、处理器、过滤器、格式器。
1)日志器(logger)需要处理器(handler)将日志输出;
2)一个logger可以有多个handler;
3)不同处理器(handler)可以将日志输出到不同位置;
4)每个handler可以有多个过滤器(filter);
5)每个处理器可以设置自己的格式器(formatter)。
3.logging日志处理流程
1)日志器等级过滤;
2)日志器的过滤器过滤;
3)日志器的处理器的等级过滤;
4)日志器的处理器的过滤器过滤。
4.配置logging的几种方式
1)显式创建loggers;
2)使用日志配置文件,使用fileConfig()读取文件内容;
3)创建包含配置信息的dict,然后传递给dictConfig()。
# 读取日志配置文件
logging.config.fileConfig('logging.conf')
# 创建一个日志器
logger = logging.getLogger('simpleExample')
# 日志输出
logger.debug('debug message')
logger.info