3
我试图检索特定用户组订阅数和订阅者数。我正在使用YouTube API for Python。检索特定用户组订阅数和订阅者数
我为订阅数写了以下代码。此代码从列表中逐一读取用户的ID,统计其订阅数量,并将ID和数字写入CSV文件。但它不能正常工作。在少数第一位用户停止在文件中写入数字之后,数字并非都是正确的。 我认为应该有比这个混乱更简单的东西。
谢谢,我感谢您的建议和意见。
import os
import gdata.youtube
import gdata.youtube.service
import time
def GetUserUrl (username):
yt_service = gdata.youtube.service.YouTubeService()
uri = 'https://gdata.youtube.com/feeds/api/users/%s/subscriptions?max-results=50&start-index=1' % username
subscription_feed = yt_service.GetYouTubeSubscriptionFeed(uri)
T1 = GetUserSub(subscription_feed)
final = 0
j = 1
total = 0
while j<800:
j = j + 50
sj = str(j)
uri = 'https://gdata.youtube.com/feeds/api/users/%s/subscriptions?max-results=50&start-index=' % username+sj
subscription_feed = yt_service.GetYouTubeSubscriptionFeed(uri)
T2 = GetUserSub(subscription_feed)
total = total + T2
final = total + T1
usersub.writelines([str(username),',',str(final),'\n'])
def GetUserSub (subscription_feed):
i = 0
for entry in subscription_feed.entry:
i = i +1
return i
usersub = open ('usersubscribtions.csv','w')
users=[]
userlist = open("user_ids_noduplicates1.txt","r")
text1 = userlist.readlines()
for l in text1:
users.append(l.strip().split()[0])
x = 0
while (x<len(users)):
try:
GetUserUrl(users[x])
time.sleep(0.4)
x = x+1
except:
usersub.writelines([str(users[x]),'\n'])
x = x+1
pass
usersub.close()
感谢马特的答案。这听起来很直接,但我不完全明白它是如何工作的。我浏览了文档:https://developers.google.com/youtube/v3/docs/channels。但我不明白我应该如何设置参数(我认为我应该将用户标识分配给'id')以供https://www.googleapis.com/youtube/v3/channels使用,并拨打电话。 – DMD 2013-02-19 12:33:23
查看我的答案,看看如何获得频道的ID,当你只有他们的用户名:http://stackoverflow.com/questions/14760497/search-youtube-videos-by-author/14761305#14761305。用户的ID是这种格式:UCDsO-0Yo5zpJk575nKXgMVA – 2013-02-19 18:15:32