文章目录
主要内容:
1.sys模块的使用
sys模块:系统相关的功能
(1)sys.exit表示程序退出,与内置函数exit功能一致
import sys
print("123")
sys.exit()
print("456")
# 123
(2)sys.path表示查找模块的路径,结果是一个列表
import sys
print("123")
print(sys.path)
# ['C:\\Users\\lenovo\\Desktop\\mypider\\spider\\spider\\spiders', 'C:\\Users\\lenovo\\Desktop\\mypider',……
(3)sys.stdin:可以像input一样,接收用户的输入
(4)sys.stdout:修改sys.stdout可以改变默认输出位置
(5)sys.stderr:修改sys.stderr可以改变错误输出的默认位置
2.math模块的使用
math:计算相关的模块
(1)math.factorial:求阶乘
import math
print(math.factorial(6)) #720
(2)math.floor:向下取整
print(math.floor(10.655)) #10
(3)math.ceil:向上取整
print(math.ceil(7.001)) #8
(4)math.pow:求幂运算
print(math.pow(2,10)) #1024.0
(5)math.sin:求sinx的值,注意:比如说求sin30度,不能写度数,而是要写math.pi/6
print(math.sin(math.pi / 6)) #弧度,180度
# 0.49999999999999994
(6)math.cos和math.tan(用法与上面方法一样)
print(math.cos(math.pi/6)) #0.8660254037844387
print(math.tan(math.pi/4)) #0.9999999999999999
3.random模块的使用
random模块:用来生成一个随机数
(1)random.randint(a,b):用来生成[a,b]的随机整数
import random
print(random.randint(1, 10))
(2)random.random():用来生成[0,1)的随机浮点数
import random
print(random.random()) #0.901817804360516
(3)random.randrange(a,b):用来生成[a,b)的随机整数,只包含开头,不包含结尾
print(random.randrange(1,10))
(4)random.choice用来在一个可迭代对象里随机抽取一个数据
print(random.choice(['zhangsan', 'lisi', 'wangwu', 'lixiaolong', 'kongming']))
(5)random.sample用来在一个可迭代对象里随机抽取n个数据,以列表的形式输出
print(random.sample(['zhangsan', 'lisi', 'wangwu', 'lixiaolong', 'kongming'],3))
# ['lisi', 'wangwu', 'lixiaolong']
4.datetime模块的使用
(1)dt.datetime.now():获取当前的运行时间
import datetime as dt
print(dt.datetime.now()) #2020-11-26 20:21:52.262582
(2)dt.date:创建一个日期
print(dt.date(2020,2,2)) #2020-02-02
(3)dt.time:创建一个时间
print(dt.time(23,11,54)) #23:11:54
(4)dt.timedelta:获取n天以后的时间
print(dt.datetime.now()+dt.timedelta(3)) #2020-11-29 20:27:03.564279
5.hashlib和hmac模块的使用
这两个模块都是对数据进行加密的。
hashlib模块里主要支持两个算法:md5和sha加密
加密方式:单向加密(只有加密过程,不能解密 md5/sha),对称加密,非对称加密(rsa)
(1)hashlib加密:
import hashlib
x=hashlib.md5() #生成一个md5对象
# 转成二进制形式('abc'.encode('utf-8'))
x.update('abc'.encode('utf-8'))
print(x.hexdigest())
#'abc'——>900150983cd24fb0d6963f7d28e17f72
#从明文到密文
推荐使用sha加密,因为它的长度更长,更加安全。sha224中的224表示位数,一个十六进制占4位
import hashlib
x=hashlib.sha1('123456'.encode())
print(x.hexdigest()) #7c4a8d09ca3762af61e59520943dc26494f8941b
x=hashlib.sha224('123456'.encode())
print(x.hexdigest()) #f8cdb04495ded47615258f9dc6a3f4707fd2405434fefc3cbf4ef4e6
x=hashlib.sha256('123456'.encode())
print(x.hexdigest()) #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
x=hashlib.sha384('123456'.encode())
print(x.hexdigest()) #0a989ebc4a77b56a6e2bb7b19d995d185ce44090c13e2984b7ecc6d446d4b61ea9991b76a4c2f04b1b4d244841449454
(2)hmac加密:
hmac加密可以指定密钥
import hmac
x=hmac.new('h'.encode(),'你好'.encode())
print(x.hexdigest())
#加密后的结果:0c3e0484c4e8ae8613b10f471a31ced8
6.uuid模块的使用
uuid用来生成一个全局唯一的id
(1)uuid3和uuid5是使用传入的字符串根据指定的算法算出来的,是固定的。uuid3是基于md5,uuid5是基于sha1
import uuid
print(uuid.uuid3(uuid.NAMESPACE_DNS,'zhangsan')) #生成固定的id,d51a47a6-3157-39a9-94d3-70ecb9fda86e
print(uuid.uuid5(uuid.NAMESPACE_DNS,'zhangsan')) #8e117ce0-eb96-5ae8-84b7-621521f751b6
(2)uuid4使用的最多
import uuid
print(uuid.uuid4()) #956bfd69-3273-4247-95e3-f1e3adfff584
7.使用pip管理第三方包
(1)pip install <package_name> :用来下载一个第三方模块
(2)pip uninstall <package_name>:用来删除一个第三方模块
(3)pip list和pip freeze:用来列出当前环境安装了哪些模块名和版本号
(4)pip install -i url <package_name> :从指定的地址下载包
8.自定义模块的使用
一个模块本质上就是一个py文件
如果一个py文件想要被当成一个py文件被导入,文件名一定要遵守命名规范:由数字、字母和下划线组成,不能以数字开头。
导入一个模块,就能使用一个模块的变量名和方法
test_xxx.py:
a='hello'
def test():
print('我是一个test方法')
def mm():
print('我是一个mm方法')
opencv.py:
from test_xxx import *
print(a) #hello
test() #我是一个test方法
mm() #我是一个mm方法
9.__name__的使用
name:当直接运行py文件的时候,值是__main__;如果这个py文件作为一个模块导入的时候,值是文件名
test_xxx.py:
a='hello'
def test():
print('我是一个test方法')
def mm():
print('我是一个mm方法')
print(__name__) #__main__
opencv.py:
from test_xxx import *
print(a)
test()
mm()
# test_xxx
# hello
# 我是一个test方法
# 我是一个mm方法
test_xxx.py:
a='hello'
def test():
print('我是一个test方法')
def mm():
print('我是一个mm方法')
print(__name__) #__main__
if __name__ == '__main__':
test()
mm()
# __main__
# 我是一个test方法
# 我是一个mm方法