原标题:Python 爬虫:time模块(获取时间信息)
Python 中的time模块是跟时间相关的模块。这个模块用得最多的地方可能就是计时器了。下面介绍这个模块最常用的几个函数。
time.time:返回当前的时间戳。
time.localtime([secs]):默认将当前时间戳转换成当前时区的struct_time。
time.sleep(secs):计时器。
Time.strftime(format[, t]):把一个struct_time转换成格式化的时间字符串。这个函数支持的格式符号如表1所示。
表1 时间字符串支持的格式符号
在使用strptime时,%p和%I配合使用才有效。%S中的秒是0~61,闰年中的秒占两秒。在使用strtime时,只有当年中的周数和天数被确定时,%U和%W才被计算。
简单地测试一下,在Windows中打开IDLE,执行命令:
import time
time.time
time.localtime
for i in range(5)
time.sleep(1)
print i
time.strftime('%Y-%m-%d %X',time.localtime)
执行结果如图1所示。