[英]Google API - Extracted comments from a google spreadsheet using Python number only 20?
我有一個包含大約3000
行的谷歌電子表格,我正在嘗試使用以下代碼從該電子表格中提取評論:
import requests
from apiclient import errors
from apiclient import discovery
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
import httplib2
CLIENT_ID = "xxxxxyyyy"
CLIENT_SECRET = "xxxxxxx"
OAUTH_SCOPE = "https://www.googleapis.com/auth/drive"
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
file-id = "zzzzzz"
def retrieve_comments(service, file_id):
"""Retrieve a list of comments.
Args:
service: Drive API service instance.
file_id: ID of the file to retrieve comments for.
Returns:
List of comments.
"""
try:
comments = service.comments().list(fileId=file_id).execute()
return comments.get('items', [])
except errors.HttpError as error:
print(f'An error occurred: {error}')
return None
# ...
flow = OAuth2WebServerFlow(CLIENT_ID,CLIENT_SECRET,OAUTH_SCOPE)
flow.redirect_uri = REDIRECT_URI
authorize_url = flow.step1_get_authorize_url()
print("Go to the following link in your web browser "+ authorize_url)
code = input("Enter verfication code : ").strip()
credentials = flow.step2_exchange(code)
http = httplib2.Http()
http = credentials.authorize(http)
service = build('drive', 'v2', http=http)
comments = retrieve_comments(service, file-id)
然而,列表comments
的長度只有20
,而電子表格肯定包含更多評論。 有人可以解釋我需要調整哪個參數來檢索電子表格中的所有評論嗎? 謝謝!
現階段,Drive API v3的“Comments: list”的maxResults
(Drive API v2)或pageSize
(Drive API v3)的默認值為20,我想這可能是你當前問題的原因However, the length of the list comments is only 20 whereas the spreadsheet surely contains more comments.
. 在這種情況下,如何進行以下修改?
comments = service.comments().list(fileId=file_id).execute()
return comments.get('items', [])
從您的腳本中,當您要使用Drive API v2時,請按如下方式進行修改。
file_id = "###" # Please set your file ID.
res = []
page_token = None
while True:
obj = service.comments().list(fileId=file_id, pageToken=page_token, maxResults=100, fields="*").execute()
if len(obj.get("items", [])) > 0:
res = [*res, *obj.get("items", [])]
page_token = obj.get("nextPageToken")
if not page_token:
break
return res
當你要使用Drive API v3時,請修改如下。
file_id = "###" # Please set your file ID.
res = []
page_token = None
while True:
obj = service.comments().list(fileId=file_id, pageToken=page_token, pageSize=100, fields="*").execute()
if len(obj.get("comments", [])) > 0:
res = [*res, *obj.get("comments", [])]
page_token = obj.get("nextPageToken")
if not page_token:
break
return res
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.