我正在为维基百科编写一个爬虫,希望将填充的结果写在csv中。有没有什么办法可以将我的程序生成的输出直接存储到Google电子表格中?
发布于 2018-03-17 14:36:18
您可以使用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} 在此之后,您需要使用以下命令将google驱动器挂载到您的实例:
!mkdir -p drive
!google-drive-ocamlfuse drive现在你应该有你所有的谷歌驱动器文件在drive文件夹中。您可以使用以下命令进行检查:
!ls drive在此之后,您可以在google驱动器中读取或写入任何文件。
发布于 2018-03-17 22:10:39
使用下面的代码片段连接到Gdrive。
您必须使用来自单元格输出的链接进行两次身份验证。
!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使用pandas阅读CSV
df = pd.read_csv('drive/path/file.csv')保存CSV
如果您不需要索引作为csv中的第一列,请使用index = False。
df.to_csv('drive/path/file.csv',index = False)https://stackoverflow.com/questions/49332876
复制相似问题