文章目录
模块
datetime
# 引包
>>> from datetime import date, datetime
# 当天日期
>>> print(date.today())
2020-09-27
# 当前时间
>>> print(datetime.now())
2020-09-27 16:51:27.293350
# 当前日期
>>> print(datetime.now().date())
2020-09-27
# 当前时间
>>> print(datetime.now().time())
16:52:16.614457
# 取单值
>>> t = datetime.now()
>>> print(t.year)
2020
>>> print(t.month)
9
>>> print(t.day)
27
>>> print(t.hour)
16
>>> print(t.minute)
52
>>> print(t.second)
38
os
os
模块提供了一些接口来获取操作系统的一些信息和使用操作系统功能。
# 引包
>>> import os
# cd 切换目录
>>> os.chdir('/root')
# pwd 当前所在目录
>>> os.getcwd()
'/root'
# ls 列出目录中的文件
>>> os.listdir('/root')
['.bash_logout', '.bash_profile', '.cshrc', '.tcshrc', '.bashrc', '.bash_history', '.python_history', '2', '.viminfo', '5', 'testfile.txt', 'course.json', 'course2.json', '4', '.ssh']
# mkdir 创建文件夹
>>> os.mkdir('test')
# touch 创建文件
>>> os.mknod('/root/123.txt')
# rm 删除文件
>>> os.remove('/root/123.txt')
# rm 删除空目录
>>> os.rmdir('/root/test')
# rename 重命名文件或目录
os.rename('course.json','course3.json')
smtplib
- 官方文档:https://docs.python.org/zh-cn/3/library/smtplib.html
- 用来发送邮件的 python 模块
sys
sys
模块可以用于获取 Python 解释器当前的一些状态变量,最常用的就是获取执行 Python 脚本时传入的参数,比如说执行 test.py
时传入了一些参数:
python3 test.py arg1 arg2 arg3
那么在 Python 程序中就可以通过 sys.argv
来获取这些参数:
sys.argv # ['test.py', 'arg1', 'arg2', 'arg3']
threading
官方文档::https://docs.python.org/zh-cn/3/library/threading.html
第三方库
bs4
官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
faker
- 官方文档:https://faker.readthedocs.io/en/master/
- 一个专门用来造假数据的第三方库
- 使用示例
>>> from faker import Faker
>>> fake = Faker(locale='zh_CN')
>>> fake.name()
'李洁'
>>> fake.address()
'上海市兴安盟县江北东莞路r座 803484'
moviepy
paramiko
- 官方文档:http://www.paramiko.org/
- paramiko是基于Python实现的SSH2远程安全连接,支持认证及密钥方式。可以实现远程命令执行、文件传输、中间SSH代理等功能,相对于Pexpect,封装的层次更高,更贴近SSH协议的功能
pyautogui
pyinstaller
pymysql
- 官方文档:https://pymysql.readthedocs.io/en/latest/
- 连接 MySQL 数据库的一个第三方库
pyqt
requests
- 官方文档:https://docs.python-requests.org/
- Requests 是一个基于 Apache2 协议开源的 Python HTTP 库,号称是“为人类准备的 HTTP 库”。他可以很轻松的发送一个 HTTP 请求,并获取返回值。
import requests
r = requests.get('https://api.github.com')
# 查看返回状态码
print(r.status_code)
# 查看返回文本
print(r.text)
# 查看返回 json 值
print(r.json)
retry
正如它的名字,retry是用来实现重试的。很多时候我们都需要重试功能,比如写爬虫的时候,有时候就会出现网络问题导致爬取失败,然后就需要重试了,也就是在函数的定义前,加一句@retry
就行了。
from retry import retry
@retry(tries=5, delay=2)
def do_something():
xxx
do_something()