iT邦幫忙

0

python抓youtubeAPI,找不同頻道的訂閱人數

import requests
import pprint

base_url = "https://www.googleapis.com/youtube/v3/"
api_function = "videos"                               
query = {
    "key":"",   
    "chart":"mostPopular",         
    "maxResults":50,
    "regionCode":"TW",
    "part": "snippet,contentDetails,statistics",

}

response = requests.get(base_url + api_function, params=query)

if response.status_code == 200:
    response_dict = response.json() 
    results1 =  {
        i+1: response_dict["items"][i]["snippet"]["channelTitle"]
        for i in range(0,50)
        } 
    results2 =  {
        i+1: response_dict["items"][i]["statistics"]["viewCount"]
        for i in range(0,50)
        } 
    results3 =  {
        i+1: response_dict["items"][i]["snippet"]["channelId"]
        for i in range(0,50)
        } 
   
# pagination
    if "nextPageToken" in response_dict.keys():
      query['pageToken'] = response_dict["nextPageToken"]
      response = requests.get(base_url + api_function, params = query)
 
      if response.status_code == 200:
        response_dict = response.json()
        for i in range(0,50):
            results1.update( {i+51: response_dict["items"][i]["snippet"]["channelTitle"] } )
      if response.status_code == 200:
        response_dict = response.json()
        for i in range(0,50):
            results2.update( {i+51: response_dict["items"][i]["statistics"]["viewCount"] } )     
      if response.status_code == 200:
        response_dict = response.json()
        for i in range(0,50):
            results3.update( {i+51: response_dict["items"][i]["snippet"]["channelId"] } )    

pprint.pprint(results1)
pprint.pprint(results2)
pprint.pprint(results3)



import requests
import pprint

base_url = "https://www.googleapis.com/youtube/v3/"
api_function = "channels"                               
query = {
    "key":"",   
    "part": "snippet,statistics",
    "id": "UCwWXGnvVmi-6Sfx2wf8S8tQ",
    
}

response = requests.get(base_url + api_function, params=query)

if response.status_code == 200:
     response_dict = response.json()
     results3_list = [ response_dict["items"][0]["statistics"]["subscriberCount"] ]
print(results3_list)  

python課程學習中~
我正在著手yt前百大影片頻道的訂閱人數,
目前有找到前百大影片的頻道名稱和個別id,但不知道怎麼寫loop去替換"id",
進而從個別頻道找到訂閱數
QQ
感謝大家的提醒!!!

看更多先前的討論...收起先前的討論...
ccutmis iT邦高手 2 級 ‧ 2021-06-29 00:14:18 檢舉
you should paste your code first.
haward79 iT邦研究生 3 級 ‧ 2021-06-29 06:44:03 檢舉
(1) 請直接貼 code
(2) 請留意資訊安全,不要給出你的 key!!!!!
haward79 iT邦研究生 3 級 ‧ 2021-06-30 08:39:52 檢舉
這個 API 可以透過你抓到的 Channel Id 查詢 channel 資訊
https://developers.google.com/youtube/v3/docs/channels
alicecila iT邦新手 5 級 ‧ 2021-06-30 11:08:08 檢舉
感謝你,我知道如何使用API,但不知道程式的部分怎麼讓他找到很多影片的資料
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
0
30858837
iT邦新手 5 級 ‧ 2021-06-29 09:00:10

留意你的資訊

0
haward79
iT邦研究生 3 級 ‧ 2021-07-01 09:01:56
import requests

def getKey() -> str:

    return 'YOUR KEY'



def queryYoutubeApi(baseUrl: str, function: str, queryParams: dict) -> requests.Response:

    queryParams['key'] = getKey()
    response = requests.get(baseUrl + function, params = queryParams)

    return response



def queryYoutube_mostPopularVideo(numResults: int) -> list:

    popularVideoSet = []

    if numResults >= 1 and numResults <= 50:
        queryParams = {
            "chart": "mostPopular",
            "maxResults": numResults,
            "regionCode": "TW",
            "part": "snippet,contentDetails,statistics"
        }

        response = queryYoutubeApi('https://www.googleapis.com/youtube/v3/', 'videos', queryParams)

        if response.status_code == 200:
            response = response.json()

            for video in response['items']:
                channelTitle = video['snippet']['channelTitle']
                viewCount = int(video['statistics']['viewCount'])
                channelId = video['snippet']['channelId']

                popularVideoSet.append({
                    'channelTitle': channelTitle,
                    'viewCount': viewCount,
                    'channelId': channelId
                })

    return popularVideoSet



def queryYoutube_channelInfo(channelIds: list):

    channelSet = []
    channelIdStr = ''

    for channelId in channelIds:
        channelIdStr += ',' + str(channelId)

    if not channelIdStr == '':
        channelIdStr = channelIdStr[1:]

        queryParams = {
            "id": channelIdStr,
            "part": "snippet,statistics"
        }

        response = queryYoutubeApi('https://www.googleapis.com/youtube/v3/', 'channels', queryParams)

        if response.status_code == 200:
            response = response.json()

            for channel in response['items']:
                id = channel['id']
                title = channel['snippet']['title']

                if channel['statistics']['hiddenSubscriberCount']:
                    subscriberCount = -1
                else:
                    subscriberCount = int(channel['statistics']['subscriberCount'])

                channelSet.append({
                    'id': id,
                    'title': title,
                    'subscriberCount': subscriberCount
                })

    return channelSet



""" main """

# Get popular videos.
popularVideoSet = queryYoutube_mostPopularVideo(50)


# Fetch channel ids.
channelIds = []

for video in popularVideoSet:
    if not video['channelId'] in channelIds:
        channelIds.append(video['channelId'])


# Get number of subscriber for each channel.
channelSet = queryYoutube_channelInfo(channelIds)

# print subcribers.
print('Total {} channels.\n'.format(len(channelSet)))

for channel in channelSet:
    if channel['subscriberCount'] >= 0:
        print('{} : {} subscriber(s)'.format(channel['title'], channel['subscriberCount']))

    else:
        print('{} : Hidden'.format(channel['title']))
看更多先前的回應...收起先前的回應...
alicecila iT邦新手 5 級 ‧ 2021-07-01 22:08:46 檢舉

謝謝你!
但我試跑過但得到的似乎不是訂閱人數?

haward79 iT邦研究生 3 級 ‧ 2021-07-02 12:24:22 檢舉

alicecila

請參考我的 執行結果截圖

我看起來輸出的的確是訂閱數啊

以輸出結果第三個「最近紅什麼」來說好了,訂閱數是 406000 沒錯,可以到 最近紅什麼的頻道 檢查訂閱數確實是 406K 沒錯。

alicecila iT邦新手 5 級 ‧ 2021-07-02 14:23:16 檢舉
haward79 iT邦研究生 3 級 ‧ 2021-07-03 06:00:26 檢舉

alicecila
請問你有把你的 key 貼進 getKey() 裡面的 YOUR KEY 那邊嗎?
會有那樣的執行結果,我推測是 api 那邊沒有成功抓到資料。

alicecila iT邦新手 5 級 ‧ 2021-07-03 19:08:13 檢舉

haward79
有哦

haward79 iT邦研究生 3 級 ‧ 2021-07-04 12:32:34 檢舉

alicecila
你有試過 debug 每個 function 執行後的結果嗎?

我要發表回答

立即登入回答