安装解析库

  • 背景说明

抓取网页代码后,下一步是从网页中获取信息。

提取信息的方法有很多,可以使用正则表达式,但是写起来比较繁琐。也可以使用强大的解析库。

此外,还有非常强大的解析方法,比如Xpath解析和CSS选择器解析等。

  • 环境说明
[root@localhost Python-3.6.6]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@localhost Python-3.6.6]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost Python-3.6.6]# getenforce 
Disabled
[root@localhost Python-3.6.6]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@localhost Python-3.6.6]# 
  • lxml安装
pip3 install lxml
  • Beautiful Soup安装
pip3 install beautifulsoup4
  • pyquery安装
pip3 install pyquery
  • tesserocr安装

爬虫过程中,经常会遇见验证码。此时我们可以直接用OCR来识别。

tesserocr是python的一个OCR识别库,其实是对tesseract做的python API的封装,所以他的核心是tesseract。所以需要先安装tesseract。

yum install -y tesseract

[root@localhost bin]# tesseract --list-langs  #查看支持的语言
List of available languages (1):
eng
#如上,只能识别英语。如果想要识别多国语言,则需要安装语言包。
yum install -y tesseract-langpack*

#安装Cython,tesserocr需要Cython>=0.23
pip3 install Cython

#安装tesserocr
pip3 install tesserocr pillow

#测试
#在网上照一张验证码的图片,存到本地。
tesseract timg.jpg result -l eng && cat result.txt
#上述方式是通过shell的方式进行测试。下面通过python的tesserocr库来测试:
>>> import tesserocr
>>> from PIL import Image
>>> image = Image.open('timg.jpg')
>>> print(tesserocr.image_to_text(image))
7364
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章