Django 用户认证 用户 邮箱登录 邮箱注册 ORM or,and,not form.py FORM ModelForm Paginator 分页 HTMl JQuery 定位元素 ajax django切片 restfulapi 跨域 Ubantu Python Mysql Scrapy 爬虫 导出 Python读写 Pycharm 破解 session re sqlit3 生成式 其他 Prism 富文本 CSS Nginx 部署 请求头 抓包 协议 selenium Ubuntu 宝塔 AI Comfy-ui ollama dify open-webui Git docker
修改所有文章的发布时间
张建行 2024年12月9日 21:30 99 文章标签: Python Mysql 其他

因为大部分文章都是从另一个网站迁移过来的,所以文章的发布时间是集中的,为了解决这个问题,所以有了下面的程序,遍历所有的文章,然后将文章的发布日期修改为随机的发布日期

本文采用Python编程语言,结合pymysql库,实现对文章发布时间的批量修改。以下是具体步骤:

  1. 导入所需库
import pymysql, time, random

 

  1. 定义获取随机发布日期的函数
def get_date():
    now = random.randint(1523203200, 1660384792)
    timeArray = time.localtime(now)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    return otherStyleTime

 

  1. 连接数据库并执行修改操作
connect = pymysql.connect(
    host='***.***.***.***',
    port=3306,
    user='***',
    password='***',
    db='***',
)
cursor = connect.cursor()
select_sql = 'SELECT * FROM blog_article WHERE id > "1"'
cursor.execute(select_sql)
for i in cursor.fetchall():
    update_sql = 'update blog_article set pub_date="%s" where id="%s"' % (get_date(), i[0])
    cursor.execute(update_sql)
    connect.commit()