Youtube 数据 Api 存储评论 URL

Youtube Data Api Storing Comments URL

我正在尝试通过数据 API 存储 YouTube 评论个人资料网址。一切正常,但我正在尝试使用 nextPageToken 浏览所有评论。一开始我可以给它设置一个值,但它不会改变,代码会进入无限循环。在循环中打印 pageToken 始终给出相同的值。

API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/commentThreads?key=MYKEY&textFormat=plainText&part=snippet&videoId=0DmA_MCIjy8&maxResults=2&order=time'
r = requests.get(url = API_ENDPOINT)
data = r.json()
for items in data['items']:
    store = str(items['snippet']['topLevelComment']['snippet']['authorChannelUrl'])
    channelUrls.append(store)

pageToken = str(data['nextPageToken'])

while pageToken:
    API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/commentThreads?key=MYKEY&textFormat=plainText&part=snippet&videoId=0DmA_MCIjy8&maxResults=2&&pageToken=' + pageToken

    pageToken= str(data['nextPageToken'])
    print(len(channelUrls))
    for items in data['items']:
        store = str(items['snippet']['topLevelComment']['snippet']['authorChannelUrl'])
        channelUrls.append(store)

您似乎在 while 循环中遗漏了对 requests.get 的调用。

试试这个:

API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/commentThreads?key=MYKEY&textFormat=plainText&part=snippet&videoId=0DmA_MCIjy8&maxResults=2&order=time'
r = requests.get(url = API_ENDPOINT)
data = r.json()
for items in data['items']:
    store = str(items['snippet']['topLevelComment']['snippet']['authorChannelUrl'])
    channelUrls.append(store)

pageToken = str(data['nextPageToken'])

while pageToken:
    API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/commentThreads?key=MYKEY&textFormat=plainText&part=snippet&videoId=0DmA_MCIjy8&maxResults=2&&pageToken=' + pageToken

    r = requests.get(url = API_ENDPOINT)
    data = r.json()
    pageToken= str(data['nextPageToken'])
    print(len(channelUrls))
    for items in data['items']:
        store = str(items['snippet']['topLevelComment']['snippet']['authorChannelUrl'])
        channelUrls.append(store)

您的代码进入无限循环,因为循环 while pageToken: 缺少对 requests.get(url = API_ENDPOINT):

的调用
while pageToken:
        API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/commentThreads?key=MYKEY&textFormat=plainText&part=snippet&videoId=0DmA_MCIjy8&maxResults=2&&pageToken=' + pageToken
        r = requests.get(url = API_ENDPOINT)
        data = r.json()
   
        pageToken = data.get('nextPageToken')
        print(len(channelUrls))
        for items in data['items']:
            store = str(items['snippet']['topLevelComment']['snippet']['authorChannelUrl'])
            channelUrls.append(store)

顺便说一句,你最好这样写 API 结果集分页:

API_ENDPOINT = 'https://www.googleapis.com/youtube/v3/commentThreads?key=MYKEY&textFormat=plainText&part=snippet&videoId=0DmA_MCIjy8&maxResults=2&order=time'
pageToken = None
channelUrls = []

while True:
    url = API_ENDPOINT
    if pageToken: url += "&pageToken=" + pageToken
    data = requests.get(url = url).json()
    for item in data.get('items', []):
        store = item['snippet']['topLevelComment']['snippet']['authorChannelUrl']
        channelUrls.append(store)
    pageToken = data.get('nextPageToken')
    if not pageToken: break

另请注意,您的 API_ENDPOINT URL 可能包含 fields 形式的请求参数:

API_ENDPOINT = '...&fields=nextPageToken,items/snippet/topLevelComment/snippet/authorChannelUrl'

从 API 中仅询问实际使用的信息始终是一个好习惯。