python3爬虫环境安装

浏览器引擎

Webkit:目前最主流的浏览器内核,webkit是苹果公司开源的浏览器内核,其前身是KHTML。基于Webkit的浏览器很多,比如Safari,Chrome,Opera
Gecko:是Firefox浏览器的内核
Trident:是IE浏览器的内核
Blink:是webkit的一个分支版本,由google开发

请求库

requests

阻塞式HTTP请求库

安装

pip install requests

selenium

自动化测试工具安装【高版本的已经不支持PhantomJS,需要使用headless】

pip install selenium==2.48.0

ChromeDriver

selenium的谷歌浏览器驱动

#官网下载地址
http://chromedriver.storage.googleapis.com/index.html

建议 windows 直接放置在python安装目录的Scripts中,如:

C:\Users\administrator\AppData\Local\Programs\Python\Python36\Scripts\chromedriver.exe

linux 或 Mac 放置在 /usr/bin 下,Mac OS X EI10.11以上需要关闭Rootless内核保护机制

mv chromedriver /usr/bin

运行测试

C:\Users\administrator>ChromeDriver
Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

GeckoDriver

selenium的火狐浏览器驱动

https://github.com/mozilla/gekodriver

环境安装测试

执行验证安装测试,跳出对应浏览器打开对应内容

# # -*- coding: utf-8 -*-
from selenium import webdriver
chrome_obj = webdriver.Chrome()                 # 打开谷歌浏览器
chrome_obj.get("https://www.baidu.com")         # 请求页面
print("获取打开的网址是:",chrome_obj.current_url)

PhantomJS

  • 无界面可脚本编程的 WebKit 浏览器引擎,支持多种Web标准,如:DOM操作、CSS选择器、JSON、Canvas以及SVG等
  • selenium支持PhantomJS,根据PhantomJS的特性,在运行时候就不会弹出浏览器了

官方网址

http://phantomjs.org

我们可以把下载好的文件目录bin下的可执行文件,如phantomjs.exe 放置在python安装目录下,也可以单独为其添加环境变量

验证测试

# # -*- coding: utf-8 -*-
from selenium import webdriver
chrome_obj = webdriver.PhantomJS()              # 打开谷歌浏览器,使用PhantomJS加载
chrome_obj.get("https://www.baidu.com")         # 请求页面
print("获取打开的网址是:",chrome_obj.current_url)

Chrome-headless 模式

适用于高版本谷歌浏览器验证测试

# # -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_obj = webdriver.Chrome(chrome_options=chrome_options)    # 打开谷歌浏览器
chrome_obj.get("https://www.baidu.com")                         # 请求页面
print("获取打开的网址是:",chrome_obj.current_url)

aiohttp

异步非阻塞式HTTP请求库

aiohttp安装

pip install aiohttp

官方推荐一起安装的字符编码检测库和DNS加速解析库安装

pip install cchardet aiodns

解析库安装

抓取网页代码后,需要从网页中提取信息,提取信息方式有很多中,可以用正则表达式提取,也可以用各种解析库提取,常用的解析库有:lxml、Beautiful Soup、pyquery等,此外还有非常强大的 XPathjiexi 和 CSS 选择器解析等等,利用解析库可以从网页中快速提取有效的信息

lxml解析库

支持HTML和XML解析,支持XPath解析方式

下载地址

https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml
或
https://pypi.org/project/lxml/#files

安装

pip install lxml-4.3.3-cp36-cp36m-win_amd64.whl

Beautiful Soup解析库

支持HTML和XML解析,它拥有强大的API和多样的解析方式

安装

pip install beautifulsoup4

安装检验

# # -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>test1</p><a>test2</>','xml')   # xml解析
print(soup.p.string)                                    # 打印xml解析中p标签

pyquery解析库

支持jQuery类似的语法来解析HTML文档,支持CSS选择器

安装

pip install pyquery

tesserocr解析库

支持OCR(Optical Character Recognition)光学字符识别,通过扫描字符,然后通过形状将其翻译成电子文本,比如验证码识别,图片转换文字等

==【下载安装注意事项:需要和本身环境的python版本和tesseract客户端版本保持一致!】==

如:

tesserocr v2.4.0 (tesseract 4.0.0) 
#代表该tesserocr版本为2.4,需要配合tesseract的版本为4.0

tesserocr v2.4.0 - Python 3.6 - 64bit
#代表该tesserocr版本为2.4,需要配合的python版本为3.6,系统为64位操作系统

在Windows下需要先下载tesseract,它为tesserocr提供服务

tesseract下载地址

https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-v4.0.0-beta.1.20180608.exe

tesseract安装

安装:略,Windows为exe格式,linux为rpm格式或tar.gz格式
注意:安装时候要安装 语言数据文件(language data files),不然运用时候会提示tessdata 路径无效,无法初始化 API

tesserocr下载地址

https://github.com/simonflueckiger/tesserocr-windows_build/releases

tesserocr安装

pip install pillow
pip install pip install tesserocr-2.4.0-cp36-cp36m-win

运行测试

# -*- coding:utf-8 -*-
import tesserocr
from PIL import Image
image=Image.open('image.jpg')
print(tesserocr.image_to_text(image))

# # -*- coding: utf-8 -*-
# import tesserocr
# print(tesserocr.file_to_text('image.jpg'))

常见错误

  • 错误信息:RuntimeError: Failed to init API, possibly an invalid tessdata path
  • 解决方案:找到Tesseract_ocr 应用(exe或rpm等)安装路径中的tessdata目录,复制到报错路径即可

转载于:https://my.oschina.net/zhaojunhui/blog/3050716

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值