Python BeautifulSoup:如何获得datwtime格式的youtube视频的发布日期时间?

Python BeautifulSoup:如何获得datwtime格式的youtube视频的发布日期时间?,python,datetime,beautifulsoup,youtube,datetime-format,Python,Datetime,Beautifulsoup,Youtube,Datetime Format,在我的爬虫程序的一部分中,我需要以youtube视频的datetime格式抓取发布的时间和日期。我正在使用bs4,到目前为止,我可以按照YT GUI向我们展示的方式获得发布的时间格式,即“2017年5月6日发布”。但我无法检索实际的日期时间。我该怎么做 我的代码: video_obj["date_published"] = video_soup.find("strong", attrs={"class": "watch-time-text"}).text return video

在我的爬虫程序的一部分中,我需要以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
输出:

    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
您可以执行以下操作以删除“发布日期”

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

pip install python-dateutil
代码:

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

一旦您获得:

Published on Feb 8, 2020
您可以执行以下操作以删除“发布日期”

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

pip install python-dateutil
代码:

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

您可以使用解析字符串并格式化输出

pubstring = video_obj["date_published"]  # "Published on Feb 8, 2020"
# pubstring[:13] cuts of first 13 chars
dt = datetime.datetime.strptime(pubstring[13:], "%b %d, %Y")
return dt.strftime("%F") # Format as needed
您可以使用解析字符串并格式化输出

pubstring = video_obj["date_published"]  # "Published on Feb 8, 2020"
# pubstring[:13] cuts of first 13 chars
dt = datetime.datetime.strptime(pubstring[13:], "%b %d, %Y")
return dt.strftime("%F") # Format as needed