简体   繁体   English

Python:如何查找谷歌驱动器文件类型?

[英]Python: How to find google drive filetype?

I'm currently using the session.get() code found at this stackoverflow. 我目前正在使用此stackoverflow中找到的session.get()代码。 When I save the drive files they don't have a filetype suffix, so I have to add one manually to based on file type to open it. 当我保存驱动器文件时,它们没有文件类型后缀,所以我必须手动添加一个文件类型以打开它。 Is there a way I can parse the chunk and get filetype variable or maybe even search by hexcode? 有没有办法可以解析块并获取filetype变量,甚至可以通过hexcode搜索? Better method? 更好的方法?

import requests
def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
    params = { 'id' : id, 'confirm' : token }
    response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

if __name__ == "__main__":
        file_id = 'TAKE ID FROM SHAREABLE LINK'
        destination = 'DESTINATION FILE ON YOUR DISK'
        download_file_from_google_drive(file_id, destination)

Source: Python: download files from google drive using url via @user6770522 来源: Python:使用url通过@ user6770522 从谷歌驱动器下载文件

I used beatifulSoup and findAll to grab the 'title' tag. 我使用了beatifulSoup和findAll来获取'title'标签。 Then applied it to destination by os path join. 然后通过os路径连接将其应用到目标。

for filename in soup.findAll('title'):
    link = filename.string
    filename = link.replace(' - Google Drive','')
    destination = os.path.join(dir,filename)
    file_path = os.path.join(year_name, destination)
    drive_pull.download_file_from_google_drive(url_id, file_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过google drive api在python的google drive中查找当前日期修改的文件和用户名? - How to find modified file and user name on current date in google drive in python by google drive api? 如何在 python 中使用 pydrive 在谷歌驱动器中查找子文件夹 ID - How to find the sub folder id in google drive using pydrive in python 如何找到谷歌驱动器的特定文件ID? - How to find a particular file id of google drive? 如何使用 python 驱动器 api 将 csv 文件上传到谷歌驱动器 - How to upload csv files to google drive using python drive api 如何使用python在Windows上查找未分配的驱动器号 - How to find a unassigned drive letter on windows with python 如何在python中向Google驱动器文件添加说明 - How to add description to a google drive file in python 如何在没有身份验证的情况下使用 python 上传到谷歌驱动器? - How to upload to google drive with python without authentication? 如何使用 Python 读取 google 驱动器文件 - How to read a google drive file with Python 在python中如何读取Google云端硬盘中的大型CSV? - In python how to read a large CSV that is in Google Drive? 如何配置Google Drive Python API - How to configure Google Drive Python API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM