Python:如何从Google驱动器读取文件?

问题描述 投票:0回答:1

我正在尝试从我的Google驱动器中读取.csv文件

import requests
from io import StringIO
import pandas as pd
orig_url='https://drive.google.com/file/d/185r4-ityWOuf-WSducqSAWm3N3A9mnXo/view?usp=sharing'
file_id = orig_url.split('/')[-2]
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
url = requests.get(dwn_url).text
csv_raw = StringIO(url)
dfs = pd.read_csv(csv_raw)

dfs.head()

<!DOCTYPE html><html><head><title>Google Drive - Virus scan warning</title><meta http-equiv="content-type" content="text/html; charset=utf-8"/><link href=&#47;static&#47;doclist&#47;client&#47;css&#47;3114378575&#45;untrustedcontent.css rel="stylesheet"><link rel="icon" href="https://ssl.gstatic.com/docs/doclist/images/infinite_arrow_favicon_4.ico"/><style nonce="NmuLcpkk+QAW7pAXfvdEPw">#gbar #guser{font-size:13px;padding-top:0px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh  .gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1    a.gb4{text-decoration:underline !important}a.gb1    a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}

0

python pandas google-drive-api
1个回答
0
投票

如该链接中所述,以下代码下载了所问问题中的文件。

Python: download files from google drive using url

import requests
import pandas as pd

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)

    print(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

    return None

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 = '185r4-ityWOuf-WSducqSAWm3N3A9mnXo'
    destination = 'file.csv'
    download_file_from_google_drive(file_id, destination)

    # open with pandas
    dfs = pd.read_csv('file.csv')
    #view head
    dfs.head()
© www.soinside.com 2019 - 2024. All rights reserved.