【发布时间】:2018-08-26 05:41:15
【问题描述】:
我正在为维基百科编写爬虫,并希望将填充的结果写入 csv。 无论如何我可以将程序生成的输出直接存储到 Google 电子表格中吗?
【问题讨论】:
标签: python google-drive-api google-colaboratory
我正在为维基百科编写爬虫,并希望将填充的结果写入 csv。 无论如何我可以将程序生成的输出直接存储到 Google 电子表格中吗?
【问题讨论】:
标签: python google-drive-api google-colaboratory
您可以使用 google-drive-ocamlfuse (https://github.com/astrada/google-drive-ocamlfuse) 将您的 google 驱动器安装到 google colab 实例上。要安装 ocamlfuse 并获得必要的权限,请运行:
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
在此之后,您需要使用以下命令将谷歌驱动器安装到您的实例:
!mkdir -p drive
!google-drive-ocamlfuse drive
现在您应该将所有 google 驱动器文件放在 drive 文件夹中。您可以使用以下方式检查:
!ls drive
在此之后,您可以在您的 google 驱动器中读取或写入任何文件。
[1]https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d
【讨论】:
您必须使用单元格输出中的链接进行两次身份验证。
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
!mkdir -p drive
!google-drive-ocamlfuse drive
df = pd.read_csv('drive/path/file.csv')
如果您不需要索引作为 csv 中的第一个列,请使用 index = False。
df.to_csv('drive/path/file.csv',index = False)
【讨论】: