gpt4 book ai didi

python - BeautifulSoup:如何以datwtime格式发布YouTube视频的日期时间?

转载 作者:行者123 更新时间:2023-12-03 05:48:40 24 4
gpt4 key购买 nike

在抓取工具的一部分中,我需要以youtube视频的datetime格式抓取发布的时间和日期。我正在使用bs4,到目前为止,我可以按照YT GUI向我们显示的方式获取已发布的时间格式,即“2017年5月6日发布”。但是我无法获取实际的日期时间。我怎样才能做到这一点?

我的代码:

    video_obj["date_published"] = video_soup.find("strong", attrs={"class": "watch-time-text"}).text
return video_obj["date_published"]

输出:
Published on Feb 8, 2020

我想要的方式:
YYYY-MM-DD HH:MM:SS

最佳答案

一旦获得:

Published on Feb 8, 2020

您可以按照以下步骤删除“发布于”
date_string = soup_string.strip("Published on")

要以YYYY-MM-DD HH:MM:SS的格式获取此文件,可以在python中使用python-dateutil库。您可以使用以下方法安装它:
pip install python-dateutil

码:
from dateutil import parser
formatted_date = parser.parse("Published on Feb 8, 2020", fuzzy=True)

这将以YYYY-MM-DD HH:MM:SS输出日期

You can read more about python-dateutil parser here

关于python - BeautifulSoup:如何以datwtime格式发布YouTube视频的日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60133764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com