前后端对接的时候,最好使用同一的时间戳 ,这样两边的处理更加灵活。
在爬虫项目中,当我们拿到了某个数据 item 的时间字段,有可能是以 js 时间戳的格式存储的。 我们在入库的时间需要做一定的准换,转换为 python 中的时间格式,进一步转换为适合插入 mysql 中的时间格式。
首先我们检查下时间是否和页面显示时间一致:
然后在 Python 程序中将这个时间戳转换为 datetime 格式的数据。
return datetime.datetime.fromtimestamp(float(originTime)/1000)
我们在建表时将时间字段设置为 datetime, 同时将 datetime 转换为 str , 就可以将其插入mysql 数据库了。
参考
https://www.jianshu.com/p/0901dd50dcdb
https://blog.csdn.net/HeatDeath/article/details/79657487
https://www.runoob.com/mysql/mysql-insert-query.html
https://blog.csdn.net/gurenyuan123/article/details/50633837
更新时间: 2019-12-30