我对google drive的工作非常陌生,我很清楚我不能要求stackoverflow提供下面场景的完整示例,但是如果你能指导我做一些类似的事情,那将会非常有帮助。我被困住了,无法继续前进。
我已经上传了7-8 gb的pdf文件的内容,其中包括pdf,docx,ppt等在谷歌驱动器。我关心的是列出包含用户查询的术语的所有文件。例如,如果我想搜索'computer vision using google drive api‘,那么结果应该包含包含术语'computer vision’的文件列表。
当我在google drive搜索框中输入一些东西时,上面的场景是可能的,下面是屏幕截图。

当我键入机器学习时,我会得到文件列表。如何通过编程检索相同的结果。我读过google drive api的文档,偶然发现syntac的‘全文本包含术语’,但我不知道如何使用它。
发布于 2019-12-30 20:36:05
正如您正确地说过的,一种简单的方法是使用请求的q参数和fullText contains X操作符。下面您可以从使用此功能的参考资料中看到Python Quickstart的改编:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = service.files().list(
pageSize=1000, fields="nextPageToken, files(id, name)", q="fullText contains 'computer vision'").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
if __name__ == '__main__':
main()注意调用service.files().list()方法时的q参数。
参考文献
https://stackoverflow.com/questions/59526277
复制相似问题