前言
手机型号:H30-T00 1G版
最初的目的:跑个scrapyd服务。
方案一:Termux,但这个要求安卓版本为5.3以上,当前手机的4.4,不支持
方案二:Linux Deploy
其他方案:网上有教程,但都未实践过。相关的关键词有:
步骤
- root手机
- 安装busybox
- 安装linux deploy
- 配置linux deploy(重点关注)
- 安装linux系统(坐等吧,手机旧的话要好久,镜像在sdcard上的话,你可能认为假死了,实际上不是的,你可以去查看镜像文件的大小,它在慢慢的增加!)
- 安装python及scrapyd配置(配置请见参考资料15)
- 后台运行scrapyd(本文使用的screen)
- 定时执行爬虫(本文使用的crontab)
配置linux deploy
- 安装系统前,打开linux deploy的设置,PATH环境中填写“/system/xbin”(这个取决与busybox的安装位置,要相同)。
- 使用镜像的方式更容易成功(实践成功),注意:镜像不能大于2G,否则会报错!“mkesfs: lseek: value too large for defined data type",如下图所示:
- 使用参考资料1中的方式导入时(目录方式安装),报错如图:
- 本人部署成功的配置方式为:chroot, debian,armhf, stretch, http://ftp.cn.debian.org/debian/,镜像,/storage/sdcard1/linux.img,2000MB,启用ssh,启用vnc。
- vnc基本没什么用(太卡,且什么都没有。。),使用ssh加ftp,几乎够用了。
在本地打开linux deploy的主机
- 安装一个终端应用
- 输入:
su
/data/data/ru.meefik.linuxdeploy/files/bin/linuxdeploy shell - 做你想做的事。。。请见参考资料2
安装python及scrapyd
linux系统上的操作:
连接ssh后,执行下列命令:
su
apt-get install python curl gcc python-dev libssl-dev screen
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install scrapyd
坐等,其中安装lxml非常慢!请注意耐心。
查看磁盘剩余空间:df -hl
PC机上的操作:
- 安装python2
- pip install virtualenv
- 创建虚拟环境:virtualenv scrapySpider
- 执行命令:scrapySpider\Scripts\activate
- 下载Twisted-18.9.0-cp27-cp27m-win32.whl 并安装
- 下载pyOpenSSL-19.0.0-py2.py3-none-any.whl并安装
- pip install pywin32
- pip install scrapyd-client
- scrapyd-deploy命令部署爬虫(windows下找不到命令,可在scrapyd-deploy文件同目录创建一个scrapyd-deploy.bat,内容为:%~dp0\python.exe %~dp0\scrapyd-deploy %* )scrapyd-deploy文件在scrapySpider\Scripts目录中。
后台运行scrapyd
下列为常用命令及快捷键,详见参考资料3
新建scrapyd环境
screen -S scrapyd
退出scrapyd环境
CTRL + A + D
查看scrapyd环境列表
screen -ls
进入到scrapyd环境:
screen -r scrapyd
定时执行爬虫
1. 配置/etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
*/35 * * * * root curl http://localhost:6800/schedule.json -d project=xxx -d spider=yyyy
#
注意:任务要添加在两个#号之前。
2. 重启cron服务(下面的命令是根据记忆写的,可能不对,请在参考资料或网上自行查找)