在下用的python3.7的环境进行配置
google data api 网址:添加链接描述
1.安装google api的工具包
pip install --upgrade google-api-python-client
pip install --upgrade google-auth-oauthlib google-auth-httplib2
2.申请Goole API密钥:
添加链接描述
在凭证中申请API密钥
3.打开Google DATA API V3的授权
添加链接描述
4.代码获取你需要的信息:
# -*- coding: utf-8 -*-
# Sample Python code for youtube.commentThreads.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/guides/code_samples#python
import os
import sys
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
import urllib
from googleapiclient.errors import HttpError
from xlwt import Workbook
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
# 头像储存目录
resourcePath = 'd:/test/'
# 图片储存目录
imgPath = resourcePath + 'img/'
# 创建存储目录
def mkDir(path):
path = path.strip()
path = path.rstrip("\\")
isExist = os.path.exists(path)
if not isExist:
os.makedirs(path)
else:
print('目录已存在!')
def downloadImg(count, url):
global imgPath
urllib.request.urlretrieve(url, imgPath + str(count) + '.jpg')
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = "YOU API SECRET KEY"
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey = DEVELOPER_KEY)# ,credentials=credentials)
pageToken = ''
mkDir(resourcePath)
mkDir(imgPath)
w = Workbook()
ws = w.add_sheet('1', cell_overwrite_ok=True)
count = 1
ws.write (0, 0, '用户昵称')
ws.write (0, 1, '用户头像地址')
name_set = {}
while True:
if count > 201:
break
request = youtube.commentThreads().list(
part="snippet",
maxResults=100,
order="orderUnspecified",
videoId="YOU VIDEO ID",
pageToken=pageToken
)
try:
response = request.execute()
# print(response['items'])
pageToken = response['nextPageToken']
for item in response['items']:
print('item name:', item['snippet']['topLevelComment']['snippet']['authorDisplayName'])
print('item name:', item['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'])
try:
if not item['snippet']['topLevelComment']['snippet']['authorDisplayName'] in name_set:
downloadImg(item['snippet']['topLevelComment']['snippet']['authorDisplayName'], item['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'])
count += 1
print('count=', count)
ws.write (count, 1, item['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'])
ws.write (count, 0, item['snippet']['topLevelComment']['snippet']['authorDisplayName'])
name_set[item['snippet']['topLevelComment']['snippet']['authorDisplayName']] = item['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']
except:
pass
w.save(resourcePath + 'userInfo.xls')
except Exception as e:
print("Unexpected error:", e)
print('no nextPageToken')
break
if __name__ == "__main__":
main()