How to set cron for regular Python scraping on Sakura server.

Introduction

It seems that you have to set cron to scrape regularly, and when I searched it, it was too confusing in an old article, so I described my own method.

I wrote about scraping last time, so if you want to scrape, please see the link below. https://qiita.com/kei_kei_kei/items/730ba09e7830d5c00a1c

Let's actually set it

Open a terminal and log in to the Sakura server.

Execute the following command.

ssh account name@Initial domain(xxx.sakura.ne.jp)

You will be asked for a password, so enter the password required when entering the domain ID. %I was able to log in when appeared. https___qiita-image-store.s3.amazonaws.com_0_154299_b9ec4b06-666d-51a7-3afe-fa0c3d5b728d.jpeg

Sakura server python version is old

Since the version of python is old, everyone is desperate to upgrade it, but it's annoying. pathIt's even more annoying because I have to change it, so leave it as it is.

However! If you move it as it is, an error will occur.

Traceback (most recent call last):
  File "train_mnist.py", line 4, in <module>
    import requests
ImportError: No module named requests

Local is Python3. Sakura is Python 2.

Locally

pip install (External module name)

I think it worked after installing it.

Sakura also needs to install an external module.

Sakura's version is too low to use pip.

If you notice, I'll try to install it with pip, but I can't.

So please install the required modules as below

python -m install requests

This time I installed requests, If you want to install Beautiful Soup, use the following command

python -m install beautifulsoup4

I will install the necessary modules like this.

If you can install the external module to be used this time, the work in the terminal is over.

We will set up the Sakura server.

Log in to the contracted Sakura server

スクリーンショット 2019-12-07 0.26.30.png

Click the cron settings link in ⓵ in the application settings

スクリーンショット 2019-12-07 0.27.38.png

Press ⓵ to go to the screen where the additional link for the new item in ⓶ appears. Just press ⓶ Add new item.

We will set how often cron is run.

スクリーンショット 2019-12-07 0.31.52.png

cd /home/My account name/www/Directory with files;<Half-width space>/usr/local/bin/python<Half-width space>対象ファイル.py



 In ⓶
 As described in the explanation, describe what time of month, what time, and every minute you want to move.
 In the image, it is executed every 2 minutes.

 Click the ⓷ submit button when you have finished entering



### Press Send to return to the cron setting screen, so set the environment variables
 ![スクリーンショット 2019-12-07 0.43.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/492078/62a9568d-dd0a-1387-5c43-a51c0b31ea65.png)
 here

 Environment variable settings: ``` PYTHONPATH```
 Environment variable values: ``` home / account name / lib / python```

 Enter the above and click Add

### Completion of registration! !!

# Let's check if it actually works.
 If you have not entered `` `1> / dev / null``` in Sakura's cron settings, you will receive an email.
 If there is an error in the email, check the error text as it is written.
 If there is an error, it will be resolved according to the error.

# Finally check required
 Sakura's Python default encoding is ascii.
 So, if you want to speak Japanese, please change the default.
 If you want to check the default encoding, go back to the beginning and open Sakura Terminal.
 Log in and type `` `python``` to execute the following command.

% python

import sys print sys.getdefaultencoding()

 You can check the default encoding.
 Since I only needed the Int type this time, I didn't have to change it, but if you need it, please try it.


## Click here for the site created with cron settings
 [Completed site: https://compadre.matrix.jp/aisekiya/](https://compadre.matrix.jp/aisekiya/)
 ![スクリーンショット 2019-12-05 16.19.32.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/492078/9a5bbedf-d217-c6d2-9311-099d82154a6c.png)



Recommended Posts

How to set cron for regular Python scraping on Sakura server.
Install Python3 on Sakura server (FreeBSD)
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
How to read pydoc on python interpreter
[Kivy] How to install Kivy on Windows [Python]
How to set the server time to Japanese time
How to erase Python 2.x on Mac.
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
How to use "deque" for Python data
How to use regular expressions in Python
How to set proxy, redirect and SSL authentication for Python Requests module
Set cron from 1 on Ubuntu 16.04 (Sakura VPS) and execute python program regularly
[2020 version] How to install Python3 on AWS EC2
Strategy on how to monetize with Python Java
How to install OpenCV on Jetson Nano Python
How to use Python Kivy ④ ~ Execution on Android ~
How to run MeCab on Ubuntu 18.04 LTS Python
How to install Python for pharmaceutical company researchers
How to set up a local development server
[Python] How to install OpenCV on Anaconda [Windows]
Building a Python environment on a Sakura VPS server
How to make Python faster for beginners [numpy]
How to set CPU affinity for process threads
How to build an environment for using multiple versions of Python on Mac
[BigQuery] How to use BigQuery API for Python -Table creation-
[For beginners] How to use say command in python!
Think about how to program Python on the iPad
Scraping 2 How to scrape
How to convert Python # type for Python super beginners: str
How to install Python
How to set up a Python environment using pyenv
How to embed mod_wsgi into Apache on Python Windows
Scraping with Python, posting on TwitterBot, regular execution on Heroku
[For beginners] How to study Python3 data analysis exam
How to run python in virtual space (for MacOS)
How to install python
How to build a Django (python) environment on docker
How to set layer on Lambda using AWS SAM
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use Django on Google App Engine / Python
Python # How to check type and type for super beginners
How to enjoy Python on Android !! Programming on the go !!
How to run Django on IIS on a Windows server
How to set the development environment for each project with VSCode + Python extension + Miniconda
How to build a Python environment on amazon linux 2
[Introduction to Python] How to write repetitive statements using for statements
Beginners use Python for web scraping (4) --2 Scraping on Cloud Shell
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to set up a simple SMTP server that can be tested locally in Python
How to use GitHub on a multi-person server without a password
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
How to build a new python virtual environment on Ubuntu
How to import Python library set up in EFS to Lambda
[python] How to use the library Matplotlib for drawing graphs
The story of how the Python bottle worked on Sakura Internet
[Hyperledger Iroha] Notes on how to use the Python SDK
Don't lose to Ruby! How to run Python (Django) on Heroku
[Ev3dev] How to display bmp image on LCD with python
How to specify a public directory Python simple HTTP server