爬虫 js 时间戳处理

前后端对接的时候,最好使用同一的时间戳 ,这样两边的处理更加灵活。

在爬虫项目中,当我们拿到了某个数据 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

发布了291 篇原创文章 · 获赞 104 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/Enjolras_fuu/article/details/103761201