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,基本上解决现有的需求,后续如果再有需求我这边会慢慢增加