Python3网络爬虫工具安装(Mac)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tinyDolphin/article/details/86362565

Python3网络爬虫工具安装(Mac)

以下都是基于 Python3

爬虫:抓取页面 -> 分析页面 -> 存储数据

请求库的安装

  • Homebrew 安装
    Mac下的包管理工具
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • Python3 安装
# Python3 & pip3 一起安装
brew install python3
  • requests 安装
    第三方库,Python不会自带这个库
pip3 install requests
  • Selenium 安装
    自动化测试工具,驱动浏览器执行特定的动作
pip3 install selenium
  • ChromeDriver 安装
    自动化测试工具,配置浏览器使用
# 查看 chrome 版本以及对应的 chromedriver 版本
http://chromedriver.chromium.org/downloads
# 配置环境变量
sudo mv chromedriver /usr/local/bin
vim ~/.bash_profile
export PATH=/usr/local/bin:$PATH
# 使环境变量生效
source ~/.bash_profile
  • aiohttp 安装
    requests 是一个 阻塞式 HTTP 请求库 ,发出一个请求后,程序会一直等待服务器响应,直到得到响应后,才会进行下一步处理。
    一个::异步 Web 服务::的库,异步操作可以借助 async/await 关键字,使写法更简洁。
    使用场景:维护一个代理池时,利用异步方式检测大量代理的运行状况,会极大地提升效率。
pip3 install aiohttp
# 官方还推荐安装以下两个库:cchardet(字符编码检测库)、aiodns(加速 DNS 的解析库)
pip3 install cchardet aiodns

解析库的安装

  • lxml 安装
    Beautiful Soup、Scrapy 框架都需要用到此库。
pip3 install lxml
# 如果产生错误,需要先执行以下命令
xcode-select --install
  • Beautiful Soup 安装
pip3 install beautifulsoup4
# 注意导入的时候
from bs4 import BeautifulSoup
  • pyquery 安装
pip3 install pyquery
  • tesserocr 安装
    爬虫遇到::验证码::时,可以直接使用 OCR 来识别。(OCR:光学字符识别)
brew install imagemagick
brew install tesseract --all-languages
pip3 install tesserocr pillow

数据库的安装

  • MySQL 安装
    轻量级关系型数据库
brew install mysql
sudo mysql.server start
sudo mysql.server stop
sudo mysql.server restart
  • MongoDB 安装
    C++ 编写的非关系型数据库,基于分布式文件存储
brew install mongodb
brew services start mongodb
sudo mongod
brew services stop mongodb
brew services restart mongodb
  • Mongo可视化工具
    RoboMongo/Robo 3T 、Studio 3T

  • Redis 安装
    基于::内存::的高效的非关系型数据库

brew install redis
brew services start redis
redis-server /usr/local/etc/redis.conf
brew services stop redis
brew services restart redis

存储库的安装

  • PyMySQL 安装
    为了 Python 与 MySQL 进行交互
pip3 install pymysql
  • PyMongo 安装
    为了 Python 与 MongoDB 进行交互
pip3 install pymongo
  • redis-py 安装
    为了 Python 与 redis 进行交互
pip3 install redis
  • RedisDump 安装
    用于 Redis 数据导入/导出的工具,基于 Ruby 实现的。
# 需要先安装 Ruby
brew install ruby
gem install redis-dump

Web 库的安装

  • Flask 安装
    轻量级的 Web 服务程序
pip3 install flask
  • Tornado 安装
    支持异步的 Web 框架,通过使用非阻塞 I/O 流,可以支撑成千上万的开发连接,效率非常高。
pip3 install tornado

App 爬取相关库的安装

web 网页数据一般是通过请求服务器的接口来获取的,但对于 APP 主要使用一些抓包技术来抓取数据。

抓包工具:
Charles & mitmproxy:简单的接口
mitmdump:复杂的接口,对抓取的请求和响应进行实时处理和保存
Appium:像 selenium 一样对 APP 进行自动化控制。

  • Charles 安装

  • mitmproxy 安装

  • Appium 安装

# 安装 node.js 
brew install node.js
# 安装 appium
npm install -g appium

爬虫框架的安装

requests、selenium 库中的组件是可以复用的,抽离出来,将各个功能模块化,就慢慢形成了爬虫框架。

  • pyspider 安装
pip3 install pyspider
# Python3.7 把 async 当作关键之,所以需要修改 pyspider 源码中的 aynsc 字段名
pyspider all # 启动
# 访问 http://localhost:5000/
# 如果安装异常,请先执行以下代码
xcode-select --install
sudo xcode-select -switch /
  • Scrapy 安装
pip3 install Scrapy
scrapy
  • Scrapy-Splash 安装
    Scrapy 中支持 JavaScript 渲染的工具、使用 Splash 的 HTTP API 进行页面渲染
# 安装 docker
brew cask install docker
# 安装 splash
docker run -p 8050:8050 scrapinghub/splash # -d 参数:以守护态运行
pip3 install scrapy-splash
  • Scrapy-Redis 安装
    scrapy 的分布式扩展模块
pip3 install scrapy-redis

部署相关库的安装

大规模抓取数据的时候,一定会用到分布式爬虫:将一份代码,同时部署到多台主机上来协同运行。
方式一:Scrapyd、Scrapyd-Client、Scrapyd API
方式二:docker 集群部署

  • Docker 安装

一种容器技术,将应用和环境等进行打包,形成一个独立的”应用”。

brew cask install docker
sudo docker run hello-world

镜像加速
默认是从国外的 Docker Hub 下载的,当然可以使用国内的镜像来加速下载

  • Scrapyd 安装
    一个用于部署和运行 Scrapy 项目的工具
pip3 install scrapyd
  • Scrapyd-Client 安装
    将 Scrapyd 代码部署到远程 Scrapyd 时,首先将代码打包为 EGG 文件,然后需要将 EGG 文件上传到远程主机。Scrapyd-Client 已经实现了这些功能。
pip3 install scrapyd-client
scrapyd-deploy -h
  • Scrapyd API 安装
    安装好 Scrapyd 之后,可以直接请求它提供的 API 来获取当前主机的 Scrapyd 任务运行状况。
pip3 install python-scrapyd-api
  • Scrapyrt 安装
    为 Scrapy 提供了一个调度的 HTTP 接口,可以直接请求 HTTP 接口来调度 Scrapy 任务。如果不需要分布式多任务的话,可以简单的使用 Scrapyrt 实现远程 Scrapy 任务的调度
pip3 install scrapyrt
# 在任意一个 Scrapy 项目中,执行以下命令来启动 HTTP 服务
scrapyrt
# 或者 Docker 启动:运行在 9080 端口,且本地 Scrapy 项目的路径为 : /home/quotesbot
docker run -p 9080:9080 -tid -v /home/user/quotesbot:/scrapyrt/poject scrapinghub/scrapyrt
  • Gerapy 安装
    一个 Scrapy 分布式管理模块
pip3 install gerapy

猜你喜欢

转载自blog.csdn.net/tinyDolphin/article/details/86362565
今日推荐