python脚本实战---修改时间戳与接口请求

涉及模块

	1、python日期与时间戳的转换
    2. 获取非当前日期时间戳
    3. 更新sql数据
    4. 接口调用

需求点

修改data_user_cpl_reward表中数据A为前一日数据
调用清除缓存接口

导入模块

import mysql.connector
import requests,json
import time
from datetime import datetime,timedelta

获取昨日当前时间戳

def yestoday():
    current_time = datetime.today()
    Adjust_time = current_time - timedelta(days=1)#时间前推一天
    untime = time.mktime(Adjust_time.timetuple())#mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数
    return  int(untime)
yt_time = yestoday()

查询当前时间

def nowTime():
    now = time.time()
    N_t = time.localtime(now)
    N_tt = time.strftime("%Y-%m-%d %H:%M:%S",N_t)#时间戳转具体日期
    return N_tt
current_time = nowTime()
print("当前时间为:",current_time)

连接数据库

config = {
   
    'host':'47.XX.XX.33',
    'user':'XX',
    'password':'NewXXXXXXTIFIED1',
    'port':'3306',
    'database':'XXX',
    'charset':'utf8'
}

检查数据库状态

try:
    cnn = mysql.connector.connect(**config)
    print("数据库连接成功")
except mysql.connector.Error as e:
    print("数据库连接失败")

更新sql表中时间戳

def update_cpa():
    cursor = cnn.cursor(buffered=True)
    try:
        update = "update data_us
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值