获取视频上传的确切时间

时间:2011-04-16 02:43:56

标签: python youtube youtube-api

我正在使用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

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

enter image description here

我也需要时间。有人知道是否可以这样做吗?

2 个答案:

答案 0 :(得分:4)

您可以使用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

答案 1 :(得分:1)

你也可以使用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>)