python同步油管用户信息

在下用的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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值