python,对提取的youtube数据进行排序

khbbv19g  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(377)

我正在努力检索youtube频道中观看率最高的前10个视频。我不太确定如何做到这一点,我相信结果会返回特定时间段的10个视频。此外,绘制条形图的y轴(“视图”)也不符合顺序。总之,我需要帮助绘制与每个视频相关的视图数量图(len(10))
获取统计数据

youtube = build('youtube', 'v3', developerKey=api_key)

request = youtube.channels().list(
        part='statistics',
        id='UC-lHJZR3Gqxt24_Td_AJ5Yw'
        )

# To get a response, use execute()

response = request.execute()

# List indices must be intergers or slices, not str

stats = response['items'][0]['statistics']
video_count = stats['videoCount']

contentdata = youtube.channels().list(
    id='UC-lHJZR3Gqxm24_Vd_AJ5Yw',
    part='contentDetails'
    ).execute()

playlist_id = contentdata['items'][0]['contentDetails']['relatedPlaylists']['uploads']
videos = []
next_page_token = None

while 1:
    res = youtube.playlistItems().list(
        playlistId=playlist_id,
        part='snippet',
        maxResults=50,
        pageToken=next_page_token
        ).execute()

    videos += res['items']
    next_page_token = res.get('nextPageToken')

    if next_page_token is None:
        break

# Get video ID for each video

video_ids = list(map(lambda x:x['snippet']['resourceId']['videoId'], videos))

# Get statistics for each video

stats = []
for i in range(0, len(video_ids), 40):
    res = youtube.videos().list(
        id=','.join(video_ids[i:i+40]), 
        part='statistics'
        ).execute()
    stats+=res['items']

views, links = [], []

for i in range(len(videos[:10])):
    try:
        title = (videos[i]['snippet']['title'])
        view = (stats[i]['statistics']['viewCount'])
        link = f"<a href='{stats[i]['id']}'>{title}</a" 

    except KeyError:
        continue

    else:
        views.append(view)
        links.append(link)

策划

from youtube_bar import links, views
from plotly.graph_objs import Bar
from plotly import offline

# Create bar graph with data

data = [{
    'type': 'bar',
    'x': links,
    'y': views,
    'opacity': 0.6,
    'marker': {
        'color': 'rgb(150, 100, 20)',
        'line': {'width':1.5, 'color': 'rgb(25, 25, 25)'}
    },
}]

my_layout = {
    'title': 'Top 10 most views for channel',
    'titlefont': {'size':28},
    'xaxis': {
        'title': 'Videos',
        'titlefont': {'size': 24},
        'tickfont': {'size': 14},
        },
    'yaxis': {
        'title': 'Views',
        'titlefont': {'size': 24},
        'tickfont': {'size': 14},
    },
}

fig = {'data': data, 'layout': my_layout}

offline.plot(fig, filename='youtube_videos.html')

图表

n9vozmp4

n9vozmp41#

代码中的循环:

for i in range(len(videos[:10])):

如果只拍摄1到10段视频(最新的,这就是为什么有“时间段”),则必须根据视频的时间对不同的视频进行排序 viewCount 在此之前。

相关问题