Python 脚本自启动及定时任务

爬虫脚本定时运行(crontab)

linux定时执行python脚本的方法以及失败不执行的处理方法

https://www.jianshu.com/p/5cd74add11ba 没有采用简书的方案,上面的博客方案更加清晰。以下是我的笔记(重点是环境变量,路径,scp的使用方法)

不需要root,执行以下命令:
crontab -e
编辑:
#注意这里最好把环境变量写上
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/chen/miniconda3/bin/
MAILTO=root
#测试:两分钟执行一次
*/2 * * * * echo "crontab test" >> /home/chen/ctest.txt
#写的python脚本每5分钟执行一次(注意python脚本中文件路径必须是绝对路径,否则报错)
*/5 * * * * /home/chen/miniconda3/bin/python /home/chen/CoV_copy2.py 
#scp python脚本产生的HTML到博客服务器(这里用了密钥而不是密码)。注意:所有的路径都#使用绝对路径,否则无效。
* * * * * scp -i /home/chen/.ssh/do248 -P 4705 /home/chen/2019-nCov数据一览2.html chen@207.148.96.113:/home/wwwroot/geoseis.cn/CoV.html >> /home/chen/cov.log
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

linux定时执行失败的处理方法就是查看 /var/spool/mail/ ,一般root里都有日志,可以看到哪里出错了。

这个爬虫脚本放到vpn后需要更新很多:

  • 文件路径必须为绝对路径
  • pyecharts的Geo函数bug:遇到不存在的地点Geo函数就会使程序退出,即使用了except也不行,和笔记本环境不一样。笔记本里运行程序不会退出。解决方案就是复制了旧版本的一些.py程序到相应的目录
  • proxy在VPN中没有设置好,因为笔记本里用的SSR代理的(5个VPN)。这个脚本五分钟执行一次不需要代理也可以

scp的使用

scp推荐使用密钥传输,更加安全。密钥产生后我直接append到目标服务器的/home/chen/.ssh/authorized_keys,然后scp -i 就可以无密码传输了。

运行 ssh-keygen -t dsa 时最好设置密码,否则密钥没有密码也不安全。

参考:

It’s better to set up ssh to used key-based authentication rather than trying to figure out how to send text to the login process with something like expect.

Take a look at:

https://help.ubuntu.com/community/SSH/OpenSSH/Keys

So, basically, run ssh-keygen -t dsa on the machine that will run your script. When it asks you for a passphrase, hit ENTER to accept a blank. You will get two files. If you followed the default suggestions, the files will be ~/.ssh/id_dsa and ~/.ssh/id_dsa.pub. The first one is the private key. The second one is the public key.

Copy the public key to the second server using ssh-copy-id user@server2. This will add the public key to the authorized_keys file of the user on server2.

You should now be able to run ssh from the first machine and log in without a password.

For copying the files, scp or rsync are fine. It depends on what you’re doing. rsync will use ssh by default, so will use the key-based authentication you just set up

pandas read_html+beautifulsoup爬虫中国产业研究院2019年三季度3688家上市公司的季报信息

今天靠港避风,大风浪对我的进度影响不大,风浪中昏睡半天后生产力反而更强大了。
两天一个小项目,今天完成了爬取3688家上市公司的2019年三季报基本信息,目标网站中国产业研究院,入门级爬虫先找个软柿子吧,目前还没研究验证码和模拟登陆。轮子造好之后可以顺利爬取N多年的季报和年报基本信息,爬完处理下可以出个小分析报告了。
还是老样子,笔记本上调试完代码爬出来输出到mysql中,因为远程v p n没有设置好mysql索性输出到了csv文件,也方便索引。
船上的龟速导致我昨天晚上代码老是掉链子,经常容易read timeout,设置了retry也不行。今天完善了下代码框架,增加了user-agent随机性,并加入了高质量的proxy(高质量vpn五个),虽然速度慢了但是稳定性加强了,可以hand free模式了。本地笔记本10秒左右有时候好几分钟,远程机器因为网速快也就3-5秒一页(太快了不稳定)。
这次用了新的方案,pandas 的read_html特别适合处理表格,+beautifulsoup解析。
400多行代码,以后可以轻松爬各种表格形式的数据了。