获取视频上传的确切时间

发布于 2024-11-01 21:06:47 字数 1503 浏览 3 评论 0原文

我正在使用 Youtube API 进行搜索关键字查询:

import gdata.youtube
import gdata.youtube.service

def youtube_query(query_text, 
        max_results=50, 
        start_index=1, 
        racy='exclude',
        orderby='relevance'):
    client = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()

    query.vq          = query_text
    query.max_results = max_results
    query.start_index = start_index
    query.racy        = racy
    #query.format = 5
    query.orderby     = orderby
    feed = client.YouTubeQuery(query)
    resultsCount = int(feed.total_results.text)
    entries = []

    try:
        while resultsCount > int(query.start_index):
            print repr(feed.entry)
            entries += feed.entry
            query.start_index = int(query.start_index) + int(query.max_results)
            feed = client.YouTubeQuery(query)
    except gdata.service.RequestError:
        #
        # Cannot request beyond 1000 items.
        #
        pass    
    return entries

每个条目都是一个 YoutubeVideoEntry< /a>.

每个视频上传的日期绝对应该是可访问的,因为每个视频都会显示该日期:

enter image这里的描述

我也需要时间。有谁知道这是否可以做到?

I'm using the Youtube API to search using a keyword query:

import gdata.youtube
import gdata.youtube.service

def youtube_query(query_text, 
        max_results=50, 
        start_index=1, 
        racy='exclude',
        orderby='relevance'):
    client = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()

    query.vq          = query_text
    query.max_results = max_results
    query.start_index = start_index
    query.racy        = racy
    #query.format = 5
    query.orderby     = orderby
    feed = client.YouTubeQuery(query)
    resultsCount = int(feed.total_results.text)
    entries = []

    try:
        while resultsCount > int(query.start_index):
            print repr(feed.entry)
            entries += feed.entry
            query.start_index = int(query.start_index) + int(query.max_results)
            feed = client.YouTubeQuery(query)
    except gdata.service.RequestError:
        #
        # Cannot request beyond 1000 items.
        #
        pass    
    return entries

Each entry is a YoutubeVideoEntry.

The date each video is uploaded should definitely be accessible, as it is displayed for every video:

enter image description here

I need the time as well. Does anybody know if this can be done?

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

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

发布评论

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

评论(2

飞烟轻若梦 2024-11-08 21:06:47

您可以使用 entry.published.text 属性:

import datetime
import time

feed = client.YouTubeQuery(query)
for entry in feed.entry:
    _tmp = time.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.000Z')
    ptime = datetime.datetime(*_tmp[:6])
    print ptime
    print ptime.time()

输出:

2010-01-27 19:42:42
19:42:42

You can use the entry.published.text attribute:

import datetime
import time

feed = client.YouTubeQuery(query)
for entry in feed.entry:
    _tmp = time.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.000Z')
    ptime = datetime.datetime(*_tmp[:6])
    print ptime
    print ptime.time()

Output:

2010-01-27 19:42:42
19:42:42
无可置疑 2024-11-08 21:06:47

您还可以使用 iso8601 库中的 parse_date 函数,该函数将返回日期对象:

import iso8601
uploaded_time = iso8601.parse_date("2007-01-25T12:00:00Z")
print uploaded_time

输出:

datetime.datetime(2013, 8, 21, 20, 54, 34, tzinfo=<iso8601.iso8601.Utc object at 0x1045d7b10>)

You can also use the parse_date function in the iso8601 library, which will return a date object:

import iso8601
uploaded_time = iso8601.parse_date("2007-01-25T12:00:00Z")
print uploaded_time

Output:

datetime.datetime(2013, 8, 21, 20, 54, 34, tzinfo=<iso8601.iso8601.Utc object at 0x1045d7b10>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文