Deploy timed crawlers on cloud servers

Posted by bloodl on Thu, 04 Nov 2021 18:39:08 +0100

github Guide

Project Address

Prepare the environment

Prepare environment:
1. Cloud Server: centos7
2.docker installation (refer to my other articles)
Originally intended to be written in the scrapy framework, but found that it is still not working, but deployment has been exhausting, so this time we are ready to make a simple version of 23333

Official start (humble version whining)

1. Write the crawler's main file locally first.

Project by

  • main function
  • Reptilian Weather
  • Scheduler scheduler

The main function and the crawler implement the crawl logic:

  • request
  • response
  • Clean
  • Preservation
    The main function adds an error capture mechanism to prevent the request from failing
from Weather import WeatherSpider
import time
    Daily crawl http://Zhuhai has the highest and lowest temperatures in
def main():
    url = ""
    # Instantiate a crawler object
    spider = WeatherSpider(url)
    # Add error capture mechanism
        # Get response
            time.sleep(60) # Take a minute off to catch
        except: # If it fails again, there's no way
  "Grab Failure")

    else: # If it's okay to get it, clean the data and save it
        # Cleaning data
        # Save data
if __name__ == '__main__':

import requests
from bs4 import BeautifulSoup
import time

class WeatherSpider:
    def __init__(self, url):
        self.url = url
        self.header = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"}

    def get_response(self):
        response = requests.get(self.url, header=self.header)
        self.text = response.text

    def clean(self):
        soup = BeautifulSoup(self.text, "html.parser")
        self.low = soup.find(id="forecastID").find_all('a')[3].find('span').text
        self.high = soup.find(id="forecastID").find_all('a')[2].find('span').text
        self.date_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    def save(self, defalut=None):
        if defalut != None:
            # Preservation
            with open("./Dayweather_log.txt", 'a') as f:
                f.write("Today's date is{},{}\n".format(self.date_time, defalut))

        # Save the temperature of the day
        with open("./Dayweather_log.txt", 'a') as f:
            f.write("Today's date is{}´╝îMaximum temperature{}, Minimum temperature{}\n".format(self.date_time, self.high, self.low))

Scheduler is designed to allow crawlers to crawl the target site at a fixed time

import schedule
import main
# Set Scheduler
# schedule.every(5) Execute job every 5s
schedule.every()"00:00").do(main.main) # Perform tasks at regular times per day
if __name__ == '__main__':
    # Evidence-Based Execution
    while True:

Target Site Screenshot:

Deploy on Server

As a qualified timer crawler, in addition to being more robust than regular crawlers (i.e., error capture as a stabilizing technique, framework is actually the best choice, but unfortunately I don't know wwww), you need a running system, so the server is the best choice. Here, I choose centos7+docker for deployment.
clone first takes a look at our project (really not advertising 2333)
git clone
Suppose you have docker installed and have a python mirror (don't go to my other blogs and do an ad)

Let's use the second to last image

Create containers:
docker run -itd -v /home/weatherspider/:/home/weatherspider centos/python-36-centos7 weatherspider_py36 bash

-d, running in the background.

Clone item:
git clone

Run in docker,
nohup python &
nohup-&: Ignore shell shutdown signal,
In this way, the python crawler can run in a completely undisturbed environment all the time!
docker top [CONTAINER ID] #Check the progress of the target container

Note that we have exited the container at this time. So successful!