Python 基础(六)常用的模块与第三方库

模块

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

  • 使用示例
>>> from faker import Faker
>>> fake = Faker(locale='zh_CN')
>>> fake.name()
'李洁'
>>> fake.address()
'上海市兴安盟县江北东莞路r座 803484'

moviepy

官方文档:http://doc.moviepy.com.cn/

paramiko

  • 官方文档:http://www.paramiko.org/
  • paramiko是基于Python实现的SSH2远程安全连接,支持认证及密钥方式。可以实现远程命令执行、文件传输、中间SSH代理等功能,相对于Pexpect,封装的层次更高,更贴近SSH协议的功能

pyautogui

官方文档:https://pyautogui.readthedocs.io/en/latest/

pyinstaller

官方文档:https://www.pyinstaller.org/

pymysql

pyqt

官方文档:https://www.riverbankcomputing.com/static/Docs/PyQt5/

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

官方文档:https://tenacity.readthedocs.io/en/latest/

正如它的名字,retry是用来实现重试的。很多时候我们都需要重试功能,比如写爬虫的时候,有时候就会出现网络问题导致爬取失败,然后就需要重试了,也就是在函数的定义前,加一句@retry就行了。

from retry import retry

@retry(tries=5, delay=2)
def do_something():
    xxx

do_something()

tqdm

官方文档:https://tqdm.github.io/

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_39680564/article/details/108831262