首页 > 其他 > 详细

安装解析库

时间:2018-11-16 20:06:02      阅读:148      评论:0      收藏:0      [点我收藏+]
  • 背景说明
  • 抓取网页代码后,下一步是从网页中获取信息。

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

    此外,还有非常强大的解析方法,比如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

    安装解析库

    原文:http://blog.51cto.com/13649299878/2318109

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
    © 2014 bubuko.com 版权所有
    打开技术之扣,分享程序人生!