Web刮擦转换UNIX时间戳到日期格式

发布于 2025-02-09 11:47:56 字数 399 浏览 2 评论 0原文

我正在尝试使用Python中的BeautifulSoup对飞行数据网站进行固定,但是时间戳是Unix Timestamp,我如何转换为常规的DateTime格式。有几个这样的列要转换。

#scheduled_departure 
result_items[0]['flight']['time']['scheduled']['departure']

输出显示为 1655781000 。我如何将其转换为 TUE,6月21日,2022年6月21日上午8:40

I'm trying to webscrape a flight data website using beautifulsoup in python but the timestamp is in unix timestamp how can i convert to regular datetime format. There are several such columns to be converted.

#scheduled_departure 
result_items[0]['flight']['time']['scheduled']['departure']

and the output is shown as 1655781000. how can I convert it to Tue, Jun 21, 2022 8:40 AM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2025-02-16 11:47:56
import time


print(time.strftime("%a, %b %d, %Y %H:%M %p", time.localtime(1655781000)))
import time


print(time.strftime("%a, %b %d, %Y %H:%M %p", time.localtime(1655781000)))
把时间冻结 2025-02-16 11:47:56

只有一个UNIX时间,它是通过使用UTC/GMT时区创建的。这意味着您可能需要转换时区来计算时间戳。

import datetime
from pytz import timezone

local_datetime = datetime.datetime.fromtimestamp(1655781000)
local_time_str = datetime.datetime.strftime(local_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Local time: {local_time_str}')

other_timezone = 'Asia/Kolkata'  # Replace your interest timezone here
remote_datetime = local_datetime.astimezone(timezone(other_timezone))
remote_time_str = datetime.datetime.strftime(remote_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Time at {other_timezone }: {remote_time_str}')

There is only one Unix time and it is created by using the UTC/GMT time zone. This means you might have convert time zones to calculate timestamps.

import datetime
from pytz import timezone

local_datetime = datetime.datetime.fromtimestamp(1655781000)
local_time_str = datetime.datetime.strftime(local_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Local time: {local_time_str}')

other_timezone = 'Asia/Kolkata'  # Replace your interest timezone here
remote_datetime = local_datetime.astimezone(timezone(other_timezone))
remote_time_str = datetime.datetime.strftime(remote_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Time at {other_timezone }: {remote_time_str}')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文