為什么使用樹莓派搭建自己的爬蟲服務(wù)器?
- 樹莓派使用Linux操作系統(tǒng),完美支持爬蟲框架Scrapy所需要的環(huán)境
- 爬蟲需要長(zhǎng)期運(yùn)行,筆記本電腦可能會(huì)隨時(shí)關(guān)閉
- 樹莓派功耗極低,就像路由器一樣打開就不用管了
準(zhǔn)備好硬件
- 樹莓派3B+
- 亞克力外殼,散熱片,風(fēng)扇
- 16G Micro-SD 卡
- 5V Micro USB 電源
- HDMI顯示器
- USB鍵盤鼠標(biāo)
先安裝散熱片

IMG_20190517_112738.jpg
在固定到底板上,一共有四個(gè)螺絲

IMG_20190517_112725.jpg
查SD卡的那一側(cè)先不安裝

IMG_20190517_112717.jpg
固定好風(fēng)扇

IMG_20190517_112704.jpg
完成!

IMG_20190517_112633.jpg
準(zhǔn)備好刷卡環(huán)境
- 一臺(tái) Windows 10, Mac 或 Linux 電腦
- Micro-SD卡 讀寫器
下載軟件
- Ubuntu Server
- 網(wǎng)址 https://www.ubuntu.com/download/iot/raspberry-pi-2-3
- 下載 樹莓派3 對(duì)應(yīng)的文件 http://cdimage.ubuntu.com/releases/bionic/release/ubuntu-18.04.2-preinstalled-server-arm64+raspi3.img.xz
- 目前(2019年5月)版本是 18.04.2
- Etcher 刷卡工具
- 網(wǎng)址 https://www.balena.io/etcher/
-
有各種系統(tǒng)的版本,Windows/MacOS/Linux
圖片.png
寫入SD卡
-
使用Etcher 將下載好的鏡像文件刷入SD卡
圖片.png
啟動(dòng)樹莓派
- 連接 HDMI顯示器, 鼠標(biāo)鍵盤
- 插入SD卡
- 連接USB電源
-
第一次啟動(dòng),用戶名與密碼都是ubuntu ,系統(tǒng)強(qiáng)制要求修改密碼
圖片.png
圖片.png
配置WiFi
- Ubuntu server 需要配置才能連wifi
- 編輯 /etc/netplan/50-cloud-init.yaml 文件: sudo vi /etc/netplan/50-cloud-init.yaml
network:
version: 2
ethernets:
eth0:
optional: true
dhcp4: true
wifis:
wlan0:
optional: true
dhcp4: true
access-points:
WIFISSID: {password: WIFIPASSWORD}
- 注意要修改WIFISSID 和WIFIPASSWORD
- 修改完成后執(zhí)行命令
sudo netplan apply
- 等待幾秒后就會(huì)連接上Wifi,執(zhí)行ifconfig 命令查看IP地址后就可以通過putty或ssh命令遠(yuǎn)程連接樹莓派了
- 如果能夠連接成功,現(xiàn)在可以取下來鍵盤鼠標(biāo)顯示器,將樹莓派放到路由器旁邊,以后就是遠(yuǎn)程管理
遠(yuǎn)程管理樹莓派
通過putty或ssh連接樹莓派
安裝需要的Python庫
sudo apt update
sudo apt install python3-pip python3-twisted python3-cffi python3-parsel python3-queuelib python3-pydispatch python3-hamcrest python3-redis
- 安裝需要的pip包
sudo pip3 install --no-deps w3lib==1.20.0 scrapy scrapyd
- 安裝完成以后,測(cè)試一下scrapy能否正常運(yùn)行
ubuntu@ubuntu:~$ scrapy
Scrapy 1.6.0 - no active project
Usage:
scrapy <command> [options] [args]
Available commands:
bench Run quick benchmark test
fetch Fetch a URL using the Scrapy downloader
genspider Generate new spider using pre-defined templates
runspider Run a self-contained spider (without creating a project)
settings Get settings values
shell Interactive scraping console
startproject Create new project
version Print Scrapy version
view Open URL in browser, as seen by Scrapy
[ more ] More commands available when run from project directory
Use "scrapy <command> -h" to see more info about a command
配置scrapyd服務(wù)器
- 在/home/ubuntu目錄下新建一個(gè)文件 scrapyd.conf
[scrapyd]
bind_address=0.0.0.0
- 執(zhí)行命令 scrapyd
ubuntu@ubuntu:~$ scrapyd
Removing stale pidfile /home/ubuntu/twistd.pid
2019-05-17T04:01:21+0000 [-] Removing stale pidfile /home/ubuntu/twistd.pid
2019-05-17T04:01:21+0000 [-] Loading /usr/local/lib/python3.6/dist-packages/scrapyd/txapp.py...
2019-05-17T04:01:22+0000 [-] Scrapyd web console available at http://0.0.0.0:6800/
2019-05-17T04:01:22+0000 [-] Loaded.
2019-05-17T04:01:22+0000 [twisted.scripts._twistd_unix.UnixAppLogger#info] twistd 17.9.0 (/usr/bin/python3 3.6.7) starting up.
2019-05-17T04:01:22+0000 [twisted.scripts._twistd_unix.UnixAppLogger#info] reactor class: twisted.internet.epollreactor.EPollReactor.
2019-05-17T04:01:22+0000 [-] Site starting on 6800
2019-05-17T04:01:22+0000 [twisted.web.server.Site#info] Starting factory <twisted.web.server.Site object at 0xffff8f405160>
2019-05-17T04:01:22+0000 [Launcher] Scrapyd 1.2.0 started: max_proc=16, runner='scrapyd.runner'
- 在筆記本上用瀏覽器打開網(wǎng)頁 http://樹莓派IP地址:6800
-
如果你看到以下內(nèi)容,恭喜,成功搭建樹莓派搭建爬蟲服務(wù)器
圖片.png
接下來的工作
- 使用scrapy開發(fā)爬蟲項(xiàng)目
- 使用gerapy、scrapydweb管理爬蟲服務(wù)器
- 使用scrapy-redis開發(fā)分布式爬蟲項(xiàng)目




