我试图使用Python检索youtube视频的评论主题。我已经打破了我的大脑并在网上寻找解决方案大约两周,但似乎没有适应我的问题。 我试图按照Youtube数据API网站上的示例进行操作,但有时候它缺乏一点清晰度。特别是在平台上调整请求以进行编码。在我的情况下,我认为我的问题是由于我不知道什么对应于' youtube'下面的代码中的对象。
import httplib2
import os
import sys
from apiclient.discovery import build_from_document
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the {{ Google Cloud Console }} at
# {{ https://cloud.google.com/console }}.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = "../***************.json"
# This OAuth 2.0 access scope allows for full read/write access to the
# authenticated user's account and requires requests to use an SSL connection.
YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the APIs Console
https://console.developers.google.com
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
# Authorize the request and store authorization credentials.
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
# Trusted testers can download this discovery document from the developers page
# and it should be in the same directory with the code.
with open("youtube-v3-discoverydocument.json", "r") as f:
doc = f.read()
return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
# Call the API's commentThreads.list method to list the existing comments.
def get_comments(youtube, video_id, channel_id):
results = youtube.commentThreads().list(
part="snippet",
videoId=video_id,
channelId=channel_id,
textFormat="plainText"
).execute()
for item in results["items"]:
comment = item["snippet"]["topLevelComment"]
author = comment["snippet"]["authorDisplayName"]
text = comment["snippet"]["textDisplay"]
print "Comment by %s: %s" % (author, text)
return results["items"]
get_comments("https://www.youtube.com/", "C_jByE6Cxv8", "UCww2zZWg4Cf5xcRKG-ThmXQ")
我一直在接受 ' STR'对象没有属性' commentThreads' 而且我想这并不是我唯一的错误,但我无法超越这个错误,所以如果有人有想法我为什么会收到此错误或者有人看到代码中的任何其他错误,请告诉我。 非常感谢Stackoverflow中的每个人,对于像我这样的新手来说,这是一个非常令人难以置信的网站。
答案 0 :(得分:1)
我将回答为什么“Youtube对象”没有属性“commentThreads”的问题:
这是因为在CLI模式程序上唯一展示的Youtube Data API V3示例中,它们使用“youtube”来命名包含函数的变量,该函数通常是身份验证函数。这就是你得到那个错误的原因。您试图将属性添加到不存在的变量,但添加到字符串对象。
您想要创建一个包含身份验证块的函数,然后创建一个名为“youtube”的变量,其中包含身份验证功能。然后它会工作。
我知道这些示例严格限于CLI风格的程序,新手很难遵循,但值得投资。继续挖掘(c:
编辑:
使它更清晰一点(这只是伪代码供你理解):
def get_auth():
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
youtube = get_auth()
youtube.commentThreads().list
然后,您将基本上将身份验证“粘合”到请求中。您基本上构建了一个发送到Youtube服务器的请求。问题是库正在为你做幕后工作,除非你深入了解库代码,否则你无法真正看到发生了什么或它是如何工作的。您将不得不提高逆向工程技能,以揭穿CLI示例的谜团。