Python 定时备份线上文件到本地文件夹

1 篇文章 0 订阅
import  requests
import time
import json
from urllib.request import urlopen
from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler

def getjson():
    json_url = 'https://www.globalviews-china.com/static/js/common.js'
    response = urlopen(json_url)
    # 读取数据
    req = response.read()
    path = 'D:\beifen'
    a = file_storage(path)
    tm = time.localtime(time.time())
    year = time.strftime('%Y', tm)
    month = time.strftime('%m', tm)
    day = time.strftime('%d', tm)
    hms = time.strftime("%H-%M", tm)
    filename = year+month+day+hms #主要是备份加前缀,防止名字相同覆盖掉前面
    # 将数据写入文件
    with open(a+'/'+filename+'-common.js', 'wb') as f:
        f.write(req)
        print(filename+"下载一次")

#下面是定时任务
scheduler = BlockingScheduler()
job = scheduler.add_job(getjson, trigger='interval', minutes=30)  # 每隔30分钟执行一次
scheduler.start()



最近有个需求,就是线上抓取数据的时候总是无缘无故的被清空,脑壳痛的想不到解决的办法,现在只能用python来自动备份一下数据,防止数据丢失能从备份中数据找回来。

首先我们要做一个自动下载的

import  requests
from urllib.request import urlopen
from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler

def getjson():
    json_url = 'https://www.globalviews-china.com/static/js/common.js'
    response = urlopen(json_url)
    # 读取数据
    req = response.read()
    path = 'D:\beifen'
    # 将数据写入文件
    with open(path+'/'+filename+'-common.js', 'wb') as f:
        f.write(req)
        print(filename+"下载一次")
#下面是定时任务
scheduler = BlockingScheduler()
job = scheduler.add_job(getjson, trigger='interval', minutes=5)  # 每隔5分钟执行一次
scheduler.start()

上面基本上能自动下载文件了,但是有个问题就是每次备份的数据都被下一个覆盖掉了,然后只能创建文件夹的方式来管理文件

下面做一个创建小时为文件夹的方式来

import os
import time

#创建以小时为单位的文件夹,看个人需求
def file_storage(file_path):
    tm = time.localtime(time.time())
    # 获取系统当前年,月,日,小时
    year = time.strftime('%Y', tm)
    month = time.strftime('%m', tm)
    day = time.strftime('%d', tm)
    hour = time.strftime('%H', tm)
    # 获取时分秒
    hms = time.strftime("%H%M%S", tm)
    # 根据当前日期创建图片文件
    file_year = file_path + '/' + year
    file_month = file_year + '/' + month
    file_day = file_month + '/' + day
    file_hour = file_day + '/' + hour
    # 判断路径是否存在,没有则创建
    if not os.path.exists(file_path):
        os.makedirs(file_path)
        os.mkdir(file_year)
        os.mkdir(file_month)
        os.mkdir(file_day)
        os.mkdir(file_hour)
    else:
        if not os.path.exists(file_year):
            os.mkdir(file_year)
            os.mkdir(file_month)
            os.mkdir(file_day)
            os.mkdir(file_hour)
        else:
            if not os.path.exists(file_month):
                os.mkdir(file_month)
                os.mkdir(file_day)
                os.mkdir(file_hour)
            else:
                if not os.path.exists(file_day):
                    os.mkdir(file_day)
                    os.mkdir(file_hour)
                else:
                    if not os.path.exists(file_hour):
                        os.mkdir(file_hour)
    return file_hour




OK,基本上解决现有的需求,后续如果再有需求我这边会慢慢增加

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值