你得到list index out of range,因为你使用了i += 1。你不需要它。
你也可以学习使用for-loop而不像range(len(...))这样
for video in allVideos:
title.append( video['snippet']['title'] )
publishedDate.append( video['snippet']['publishedAt'] )
如果您需要for-loop 中的号码,那么您可以使用enumerate
for number, video in enumerate(allVideos):
title.append( video['snippet']['title'] )
publishedDate.append( video['snippet']['publishedAt'] )
comment.append( int(stats[number]['statistics']['commentCount']) )
liked.append( int(stats[number]['statistics']['likeCount']) )
但在您的代码中,您可以使用 zip() 来更简单地完成它
for video, stat in zip(allVideos, stats):
title.append( video['snippet']['title'] )
publishedDate.append( video['snippet']['publishedAt'] )
comment.append( int(stat['statistics']['commentCount']) )
liked.append( int(stat['statistics']['likeCount']) )
如果您首先获得 ['snippet'] 和 ['statistics'] 并分配给变量,您可以使其更具可读性
for video, stat in zip(allVideos, stats):
v = video['snippet']
title.append( v['title'] )
publishedDate.append( v['publishedAt'] )
s = stat['statistics']
comment.append( int(s['commentCount']) )
liked.append( int(s['likeCount']) )
如果你得到 KeyError 那么你应该使用if/else
s = stat['statistics']
if 'commentCount' in s:
comment.append( int(s['commentCount']) )
else:
comment.append( 0 )
if 'likeCount' in s:
liked.append( int(s['likeCount']) )
else:
liked.append( 0 )
使用.get(key, default value)或更短
s = stat['statistics']
comment.append( int(s.get('commentCount', 0)) )
liked.append( int(s.get('likeCount', 0)) )
完整版:
for video, stat in zip(allVideos, stats):
v = video['snippet']
title.append( v['title'] )
publishedDate.append( v['publishedAt'] )
video_description.append( v['description'] )
videoid.append( v['resourceId']['videoId'] )
s = stat['statistics']
liked.append( int(s.get('likeCount',0)) )
disliked.append( int(s.get('dislikeCount',0)) )
views.append( int(s.get('viewCount',0)) )
comment.append( int(s.get('commentCount',0)) )
但如果您想将其添加到 DataFrame 中,那么您可以在没有所有这些列表 title 等的情况下使其更简单。
all_rows = []
for video, stat in zip(allVideos, stats):
v = video['snippet']
s = stat['statistics']
row = [
v['title'],
v['resourceId']['videoId'],
v['description'],
v['publishedAt'],
int(s.get('likeCount',0)),
int(s.get('dislikeCount',0)),
int(s.get('viewCount',0)),
int(s.get('commentCount',0)),
]
all_rows.append(row)
# - after `for`-loop -
df = pd.DataFrame(
all_rows,
columns=['title', 'videoIDS', 'video_description', 'publishedDate', 'likes', 'dislikes', 'views', 'comment']
)
编辑:
完整的工作代码:
from googleapiclient.discovery import build
import pandas as pd
youTubeApiKey = "AIzaSyCoBcCAxIGkTf5WKxAiXJu48APdyQjqU0I"
youtube = build('youtube', 'v3', developerKey=youTubeApiKey)
snippets = youtube.search().list(part="snippet", type="channel", q="nptelhrd").execute()
print('len(snippets):', len(snippets))
channel_id = snippets['items'][0]['snippet']['channelId']
print('channel_id:', channel_id)
stats = youtube.channels().list(part="statistics", id = channel_id).execute()
print('len(stats):', len(stats))
#status = youtube.channels().list(id=channel_id, part='status').execute()
#print('len(status):', len(status))
content = youtube.channels().list(id=channel_id, part='contentDetails').execute()
print('len(content):', len(content))
upload_id = content['items'][0]['contentDetails']['relatedPlaylists']['uploads']
print('upload_id:', upload_id)
all_videos = []
next_page_token = None
number = 0
while True:
number +=1
print('page', number)
res = youtube.playlistItems().list(playlistId=upload_id, maxResults=50, part='snippet', pageToken=next_page_token).execute()
all_videos += res['items']
next_page_token = res.get('nextPageToken')
if next_page_token is None:
break
print('len(all_videos):', len(all_videos))
video_ids = list(map(lambda x: x['snippet']['resourceId']['videoId'], all_videos))
print('len(video_ids):', len(video_ids))
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']
print('len(stats):', len(stats))
all_rows = []
number = 0
for video, stat in zip(all_videos, stats):
number +=1
print('row', number)
v = video['snippet']
s = stat['statistics']
row = [
v['title'],
v['resourceId']['videoId'],
v['description'],
v['publishedAt'],
int(s.get('likeCount',0)),
int(s.get('dislikeCount',0)),
int(s.get('viewCount',0)),
int(s.get('commentCount',0)),
]
all_rows.append(row)
# - after `for`-loop -
df = pd.DataFrame(all_rows, columns=['title', 'videoIDS', 'video_description', 'publishedDate', 'likes', 'dislikes', 'views', 'comment'])
print(df.head())
结果:
title videoIDS ... views comment
0 NPTEL Awareness workshop in association with P... TCMQ2NEEiRo ... 7282 0
1 Cayley-Hamilton theorem WROFJ15hk00 ... 3308 4
2 Recap of matrix norms and Levy-Desplanques the... WsO_s8dNfVI ... 675 1
3 Convergent matrices, Banach lemma PVGeabmeLDQ ... 676 2
4 Schur's triangularization theorem UbDwzSnS0Y0 ... 436 0
[5 rows x 8 columns]