time模块
该模块是安装好python后自带的模块之一。
引用模块
import time
使用
当前时间戳
import time
t1 = time.time()
print(t1) # 1599807910.0991206
时间格式化成2020-03-20 11:45:39形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
这种形式用得比较多。
pipreqs
pipreqs可以帮你找到当前项目的所有组件及其版本。就是当别人给你一个程序的时候,你发现别人给你的项目没有项目依赖文件,那这个时候你要在自己电脑上运行起来,就需要安装项目所依赖的模块,总不能自己一个一个找吧。
使用步骤
先安装模块
pip install pipreqs
生成项目的包文件
- 在项目根目录下执行命令
pipreqs ./
如果遇到报错就执行下面这条
pipreqs ./ --encoding=utf-8
如果你的./目录中已经有了requirements.txt这个文件,那么你想更新怎么办呢?命令如下:
pipreqs ./ --force
--force 强制的意思。
- 可以在
./路径下看到生成了requirements.txt文件 - 执行下面代码就会把项目用到的所有模块安装在虚拟环境中
-
pip3 install -r requirements.txt
-
pytesseract
介绍
OCR技术是光学字符识别的缩写(Optical Character Recognition),是通过扫描等光学输入方式将各种票据、报刊、书籍、文稿及其它印刷品的文字转化为图像信息,再利用文字识别技术将图像信息转化为可以使用的计算机输入技术。可应用于银行票据、大量文字资料、档案卷宗、文案的录入和处理领域。
环境
采用ubuntu。
安装
安装 tesseract-ocr 包
sudo apt-get install tesseract-ocr
安装pytesseract和PIL库
pytesseract
pip install pytesseract
PIL
pip install pillow
图片识别使用
非中文图片识别
import pytesseract
from PIL import Image
def read_text(image_path):
image = Image.open(image_path)
text = pytesseract.image_to_string(image=image)
print(text)
if __name__ == '__main__':
image_path = 'ddd.jpg'
read_text(image_path)
中文图片识别
要想识别的中文需要添加中文字库,需要在ubuntu 系统中 找到 tessdata 文件夹把中文字库放进去,也可以在线安装中文字库:
sudo apt-get install tesseract-ocr-chi-sim
代码:
# pip show pytesseract
import pytesseract
from PIL import Image
def read_text(image_path):
image = Image.open(image_path)
text = pytesseract.image_to_string(image=image,lang='chi_sim')
print(text)
if __name__ == '__main__':
image_path = 'dddeee.jpg'
read_text(image_path)
命令行识别
# 识别英文:
tesseract e.png 1 #1 是存储获取内容的文件,会在本地生成一个1文件
# 识别中文
tesseract --help # 查看帮助
tesseract --list -langs # 查看是否安装了中文库chi_sim
tesseract -l chi_sim c.png 1 # 1也是结果的文件把识别的结果存到此文件中
Tesseract
如果你是在windows下开发的话,可能要用到如下的某个安装包:
随机请求头库
安装
pip install fake-useragent
使用
from fake_useragent import UserAgent
ua = UserAgent()
print("chrome浏览器任意版本", ua.chrome)
print("ie浏览器任意版本", ua.ie)
print("firefox浏览器任意版本", ua.firefox)
print("任意厂家的浏览器", ua.random)
在写爬虫的时候可能就会用到这个库了。