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

當前Docker容器配置:python

  • Centos7.4
  • python2.7.5

目標Docker容器配置:linux

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

安裝依賴環境

[root@bf8feb8d5089 /]# 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

建立目錄存放安裝包

[root@bf8feb8d5089 /]# mkdir /usr/local/download 
[root@bf8feb8d5089 /]# cd /usr/local/download

安裝Python3.7.4

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

# 編譯
[root@bf8feb8d5089 download]# cd Python-3.7.4

[root@bf8feb8d5089 Python-3.7.4]# ./configure 


# 編譯和安裝
[root@bf8feb8d5089 Python-3.7.4]# make && make install

# 備份源文件
[root@bf8feb8d5089 Python-3.7.4]# mv /usr/bin/python /usr/bin/python.bak

# 軟鏈接
[root@bf8feb8d5089 Python-3.7.4]# find / -name python3
/usr/local/bin/python3
[root@bf8feb8d5089 Python-3.7.4]# ln -s /usr/local/bin/python3 /usr/bin/python
[root@bf8feb8d5089 Python-3.7.4]# ln -s /usr/local/bin/pip3 /usr/bin/pip


# 修改yum文件(由於yum是python2寫的)
[root@bf8feb8d5089 Python-3.7.4]# vi /usr/bin/yum
將第一行python改成python2.7
若是存在/usr/libexec/urlgrabber-ext-down,則將其中的python也改了


# 配置pip源
[root@bf8feb8d5089 Python-3.7.4]# cd 
[root@bf8feb8d5089 ~]# mkdir .pip
[root@bf8feb8d5089 ~]# vi .pip/pip.conf
#寫入以下內容
[global]
index-url=http://pypi.douban.com/simple
trusted-host = pypi.douban.com

根據需求安裝所需包

[root@bf8feb8d5089 ~]# pip install requests
[root@bf8feb8d5089 ~]# pip install Pillow
[root@bf8feb8d5089 ~]# pip install httplib2
[root@bf8feb8d5089 ~]# pip install excel

安裝tesseract

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


# 安裝tesseract-3.04
[root@bf8feb8d5089 leptonica-1.72]# cd ..
[root@bf8feb8d5089 download]# wget https://github.com/tesseract-ocr/tesseract/archive/3.04.zip
[root@bf8feb8d5089 download]# unzip 3.04.zip && cd tesseract-3.04/
[root@bf8feb8d5089 tesseract-3.04]# ./configure
[root@bf8feb8d5089 tesseract-3.04]# make && make install
# 手動更新動態連接庫
[root@bf8feb8d5089 tesseract-3.04]# ldconfig
[root@bf8feb8d5089 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

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

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

# 安裝geckodriver
[root@bf8feb8d5089 download]# wget https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz
[root@bf8feb8d5089 download]# tar xvzf geckodriver-*.tar.gz
[root@bf8feb8d5089 download]# rm -f /usr/bin/geckodriver
[root@bf8feb8d5089 download]# ln -s /usr/local/download/geckodriver /usr/bin/geckodriver    # 軟連接必須用絕對路徑

測試用例:c++

#!/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
在這裏插入圖片描述git

相關文章
相關標籤/搜索