在手机上部署运行scrapyd服务

前言

手机型号:H30-T00 1G版

最初的目的:跑个scrapyd服务。

方案一:Termux,但这个要求安卓版本为5.3以上,当前手机的4.4,不支持

方案二:Linux Deploy

其他方案:网上有教程,但都未实践过。相关的关键词有:

步骤

  1. root手机

  2. 安装busybox

  3. 安装linux deploy

  4. 配置linux deploy(重点关注)

  5. 安装linux系统(坐等吧,手机旧的话要好久,镜像在sdcard上的话,你可能认为假死了,实际上不是的,你可以去查看镜像文件的大小,它在慢慢的增加!)

  6. 安装python及scrapyd配置(配置请见参考资料15)

  7. 后台运行scrapyd(本文使用的screen)

  8. 定时执行爬虫(本文使用的crontab)

配置linux deploy

  1. 安装系统前,打开linux deploy的设置,PATH环境中填写“/system/xbin”(这个取决与busybox的安装位置,要相同)。

  2. 使用镜像的方式更容易成功(实践成功),注意:镜像不能大于2G,否则会报错!“mkesfs: lseek: value too large for defined data type",如下图所示:

     

  3. 使用参考资料1中的方式导入时(目录方式安装),报错如图:

     

  4. 本人部署成功的配置方式为:chroot, debian,armhf, stretch, http://ftp.cn.debian.org/debian/,镜像,/storage/sdcard1/linux.img,2000MB,启用ssh,启用vnc。

  5. vnc基本没什么用(太卡,且什么都没有。。),使用ssh加ftp,几乎够用了。

在本地打开linux deploy的主机

  1. 安装一个终端应用

  2. 输入:
    su

    /data/data/ru.meefik.linuxdeploy/files/bin/linuxdeploy shell

  3. 做你想做的事。。。请见参考资料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机上的操作:

  1. 安装python2

  2. pip install virtualenv

  3. 创建虚拟环境:virtualenv scrapySpider

  4. 执行命令:scrapySpider\Scripts\activate

  5. 下载Twisted-18.9.0-cp27-cp27m-win32.whl 并安装

  6. 下载pyOpenSSL-19.0.0-py2.py3-none-any.whl并安装

  7. pip install pywin32

  8. pip install scrapyd-client

  9. 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服务(下面的命令是根据记忆写的,可能不对,请在参考资料或网上自行查找)

/etc/init.d/cron restart

参考资料

  1. https://mlapp.cn/319.html(实践:这个很有帮助,但目前2.1版本中没有免费的可导入的了,使用文中的方式可以下载tgz包,但安装不成功,在执行tar命令解压的时候就出错了)

  2. http://moonwwdz.github.io/2018/04/01/deploy-linux-root/   (很好的思路,不过我的安装完成后,su命令有效)

  3. https://blog.csdn.net/dream8062/article/details/78875722

  4. https://www.cnblogs.com/liuliliuli2017/p/6746440.html

  5. https://www.cnblogs.com/zhoujinyi/p/6073705.html

  6. https://www.cnblogs.com/intval/p/5763929.html

  7. https://www.cnblogs.com/technologylife/p/6635631.html

  8. https://blog.csdn.net/the_fool_/article/details/79926606

  9. https://my.oschina.net/zss1993/blog/1790223?tdsourcetag=s_pctim_aiomsg

  10. https://blog.csdn.net/u010060544/article/details/79878234(使用wget下载文件时使用)“wget http... --no-check-certificate”

  11. https://blog.csdn.net/wangyezi19930928/article/details/12004723?utm_source=blogxgwz6(这个自行编译了,未使用,本文使用apt-get install python安装)

  12. https://blog.csdn.net/Marksinoberg/article/details/79546273#%E9%83%A8%E7%BD%B2%E7%88%AC%E8%99%AB

  13. https://www.jianshu.com/p/1337f2b29518

  14. http://www.miui.com/thread-9032970-1-1.html

  15. https://scrapyd.readthedocs.io/en/stable/config.html#config-example(scrapyd配置及说明)

  16. https://blog.csdn.net/web_9705/article/details/80417219

遗留问题

  1. 2000MB的空间有些小, 想办法扩容?

  2. 只运行一个scrapyd服务是否浪费了?考虑网盘?网站?绑定域名?

  3. 打包PC上的python虚拟环境,因为每次安装太麻烦。
  4. 打包linux系统上的scrapyd的python环境,这个安装更慢。
  5. 备份linux系统,这个安装太不容易了,我不想再来一遍。

转载于:https://www.cnblogs.com/FairlyHarmony/p/10398671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值