2012-10-10 2 views
5

한 번에 한 페이지 씩 이동하지 않고 특정 동영상의 모든 댓글을 수집하려고합니다.Python의 gdata 모듈로 모든 YouTube 댓글을 얻는 방법은 무엇입니까?

from gdata import youtube as yt 
from gdata.youtube import service as yts 

client = yts.YouTubeService() 
client.ClientLogin(username, pwd) #the pwd might need to be application specific fyi 

comments = client.GetYouTubeVideoComments(video_id='the_id') 
a_comment = comments.entry[0] 

단일 주석, 가능성이 가장 최근의 코멘트를 잡아 보자와 위의 코드,하지만 난 한 번에 모든 코멘트를 잡기 위해 방법을 찾고 있어요. 파이썬의 gdata 모듈로 가능합니까? 수천있을 때


유튜브 API 문서 comments에 대한 주석 공급 docs과 파이썬 API docs

+0

이 답변 [이 질문에 대한 답변 (http://stackoverflow.com/questions/10941803/using-youtube-api-to-all-comments-from-a-video-with-the-json-feed) YouTube PHP API에는이를 허용하는 호출이 있으므로 PHP를 사용하는 솔루션으로 필자는 순수한 Python 응답이 없다고 생각합니다. –

+0

@KenB 나는 그것을 보았다. 그것은 수치 스럽습니다. 문제의 비디오에는 9k 개의 댓글이 있으며 360 GetNextLink 호출이 최선의 방법이라고 생각하지 않습니다. – TankorSmash

+1

'www.youtube.com/all_comments? v = video_id' URL은 분석 가능한 댓글 목록이 있지만 긴로드 시간입니다. 내가 그것을 시도 할 수 있다고 가정 해보자. – TankorSmash

답변

7

bs4을 가진 후 다음은 당신이 Python YouTube API 사용에 대한 질문 무엇을 달성 :

from gdata.youtube import service 

USERNAME = '[email protected]' 
PASSWORD = 'a_very_long_password' 
VIDEO_ID = 'wf_IIbT8HGk' 

def comments_generator(client, video_id): 
    comment_feed = client.GetYouTubeVideoCommentFeed(video_id=video_id) 
    while comment_feed is not None: 
     for comment in comment_feed.entry: 
      yield comment 
     next_link = comment_feed.GetNextLink() 
     if next_link is None: 
      comment_feed = None 
     else: 
      comment_feed = client.GetYouTubeVideoCommentFeed(next_link.href) 

client = service.YouTubeService() 
client.ClientLogin(USERNAME, PASSWORD) 

for comment in comments_generator(client, VIDEO_ID): 
    author_name = comment.author[0].name.text 
    text = comment.content.text 
    print("{}: {}".format(author_name, text)) 

불행하게도 API가 1000에 검색 할 수있는 항목의 수를 제한합니다. 같은 원리가 API의 다른 피드에서 항목을 검색에 적용해야

gdata.service.RequestError: {'status': 400, 'body': 'You cannot request beyond item 1000.', 'reason': 'Bad Request'} 

참고 :이 내가 손으로 불통 버전을 시도 할 때 내가 가진 오류가 GetYouTubeVideoCommentFeed URL 매개 변수를 제작했다. 당신이 GetYouTubeVideoCommentFeed URL 매개 변수를 정교하게 손하려면

, 그 형식은 다음과 같습니다

'https://gdata.youtube.com/feeds/api/videos/{video_id}/comments?start-index={sta‌​rt_index}&max-results={max_results}' 

다음과 같은 제한 사항이 적용됩니다 start-index <= 1000max-results <= 50.

+1

을 찾으면 내 대답을 편집하십시오. 'start_index' 또는'items_per_page'를 수동으로 설정하는 방법이 있는지 알고 있습니까? 첫 번째 주석 집합에 설정하면 아무 것도하지 않는 것처럼 보입니다. – TankorSmash

+1

'GetYouTubeVideoCommentFeed'에 다음 형식의 URL을 전달하면됩니다. https://gdata.youtube.com/feeds/api/videos/{video_id}/comments?start-index={start_index}&max-results = {max_results}'. 다음 제한이 적용된다 :'start-index <= 1000'과'max-results <= 50'. –

+0

위대한, 심지어 URI를 변경 생각하지 않았다, 환호! – TankorSmash

1

내가 지금 가지고있는 유일한 솔루션,하지만 그것은 API를 사용하여 느린 도착 아니에요 코멘트. here을 볼 때 당신이 딕셔너리를 사용하고 있기 때문에 때문에 '클래스'는 내장 파이썬 이름 인에, 당신은 일반 매개 변수를 통해, 정규식 또는 람다를 통해 'startwith'에 대한 일반 검색을 수행 할 수없는

import bs4, re, urllib2 
#grab the page source for vide 
data = urllib2.urlopen(r'http://www.youtube.com/all_comments?v=video_id') #example XhFtHW4YB7M 
#pull out comments 
soup = bs4.BeautifulSoup(data) 
cmnts = soup.findAll(attrs={'class': 'comment yt-tile-default'}) 
#do something with them, ie count them 
print len(cmnts) 

주 . BeautifulSoup 때문에 꽤 느려지지만 어떤 이유로 든 etreeminidom이 일치하는 태그를 찾지 않아 사용해야합니다. 심지어 prettyfying()

+0

안녕하세요, 관심있는 대답은하지만 그 html 구조가 변경된 것 같아요. 'comment yt-tile-default' 대신에 대체 태그를 사용합니까? 고맙습니다! – Thoth

+0

@이 질문을 잠시 사용하지 않았지만 개발자 도구를 열고 – TankorSmash