簡體   English   中英

Python:如何查找谷歌驅動器文件類型?

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

我目前正在使用此stackoverflow中找到的session.get()代碼。 當我保存驅動器文件時,它們沒有文件類型后綴,所以我必須手動添加一個文件類型以打開它。 有沒有辦法可以解析塊並獲取filetype變量,甚至可以通過hexcode搜索? 更好的方法?

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)

來源: Python:使用url通過@ user6770522 從谷歌驅動器下載文件

我使用了beatifulSoup和findAll來獲取'title'標簽。 然后通過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM