【docker】CentOS7.4+Python3.7+selenium+Firefox+tesseract的搭建

明天你好 2020-01-08

当前Docker容器配置:

  • Centos7.4
  • python2.7.5

目标Docker容器配置:

  • Centos7.4
  • python3.7.4
  • selenium 3.141.0
  • geckodriver 0.15
  • firefox 56.0.2
  • Pillow 6.1.0
  • pytesseract 0.2.7

安装依赖环境

[ /]# yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make wget git unzip gcc gcc-c++ libjpeg-devel libpng-devel libgif-devel

创建目录存放安装包

[ /]# mkdir /usr/local/download 
[ /]# cd /usr/local/download

安装Python3.7.4

[ /]# cd /usr/local/download
[ download]# wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
[ download]# tar -xvf Python-3.7.4.tgz

# 编译
[ download]# cd Python-3.7.4

[ Python-3.7.4]# ./configure 


# 编译和安装
[ Python-3.7.4]# make && make install

# 备份源文件
[ Python-3.7.4]# mv /usr/bin/python /usr/bin/python.bak

# 软连接
[ Python-3.7.4]# find / -name python3
/usr/local/bin/python3
[ Python-3.7.4]# ln -s /usr/local/bin/python3 /usr/bin/python
[ Python-3.7.4]# ln -s /usr/local/bin/pip3 /usr/bin/pip


# 修改yum文件(因为yum是python2写的)
[ Python-3.7.4]# vi /usr/bin/yum
将第一行python改为python2.7
如果存在/usr/libexec/urlgrabber-ext-down,则将其中的python也改了


# 配置pip源
[ Python-3.7.4]# cd 
[ ~]# mkdir .pip
[ ~]# vi .pip/pip.conf
#写入如下内容
[global]
index-url=http://pypi.douban.com/simple
trusted-host = pypi.douban.com

根据需求安装所需包

[ ~]# pip install requests
[ ~]# pip install Pillow
[ ~]# pip install httplib2
[ ~]# pip install excel

安装tesseract

# 安装leptonica
[ ~]# cd /usr/local/download/
[ download]# wget http://www.leptonica.org/source/leptonica-1.72.tar.gz
[ download]# tar xvzf leptonica-1.72.tar.gz
[ download]# cd leptonica-1.72/
[ leptonica-1.72]# ./configure
[ leptonica-1.72]# make && make install


# 安装tesseract-3.04
[ leptonica-1.72]# cd ..
[ download]# wget https://github.com/tesseract-ocr/tesseract/archive/3.04.zip
[ download]# unzip 3.04.zip && cd tesseract-3.04/
[ tesseract-3.04]# ./configure
[ tesseract-3.04]# make && make install
# 手动更新动态链接库
[ tesseract-3.04]# ldconfig
[ tesseract-3.04]# pip install pytesseract

# 安装语言包
在https://github.com/tesseract-ocr/tessdata 下载对应语言的模型文件
由于目前只需要识别手机号码和英文,只下载一个eng.traineddata文件即可,
将模型文件移动到/usr/local/share/tessdata
然后即可进行识别

# 示例
import pytesseract
from PIL import Image

image = Image.open('bb.png')
code = pytesseract.image_to_string(image)
print(code)

安装selenium+Firefox+Xvfb

[ tesseract-3.04]# yum install -y Xvfb gtk3 gtk3-devel libXfont xorg-x11-fonts* libgtk-3.so.0 bzip2 
[ tesseract-3.04]# pip install xvfbwrapper selenium pyvirtualdisplay

# 安装浏览器
[ tesseract-3.04]# cd /usr/local/download/
[ download]# wget https://ftp.mozilla.org/pub/firefox/releases/56.0.2/linux-x86_64/en-US/firefox-56.0.2.tar.bz2
[ download]# tar xjvf firefox-56.0.2.tar.bz2
[ download]# rm -f /usr/bin/firefox
[ download]# ln -s /usr/local/download/firefox/firefox /usr/bin/firefox

# 安装geckodriver
[ download]# wget https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz
[ download]# tar xvzf geckodriver-*.tar.gz
[ download]# rm -f /usr/bin/geckodriver
[ download]# ln -s /usr/local/download/geckodriver /usr/bin/geckodriver    # 软链接必须用绝对路径

测试用例:

#!/usr/bin/python
# -*- coding:utf-8 -*-
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
display = Display(visible=0, size=(800,600))
display.start()
binary = FirefoxBinary('/usr/bin/firefox')
driver = webdriver.Firefox(firefox_binary=binary)
driver.get('https://www.baidu.com')
print(driver.title)
driver.quit()
display.stop()

关注公众号西加加先生一起玩转Python
【docker】CentOS7.4+Python3.7+selenium+Firefox+tesseract的搭建

相关推荐