网页抓取将Unix时间戳转换为日期格式



我试图在python中使用beautifulsoup web刮取航班数据网站,但时间戳是在unix时间戳中,我如何转换为常规日期时间格式。有几个这样的列要转换。

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

,输出显示为1655781000。如何将其转换为

import time

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

只有一个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}')

最新更新