简体   繁体   中英

Export csv file from collab notebook to google drive

I am migrating from Visual Code to Google Colab however all my attempts to write a CSV file in the Goggle Drive have not been successful. I read some topics here on similar queries, however, I was not able to mount the code correctly - as I am getting this silliest error.

Code ran on Visual Code:

import facebook_scraper
import pandas as pd
import csv
from facebook_scraper import get_posts

listaBibliotecas = ["bibliotecafoa"]

for biblioteca in listaBibliotecas:
  print("Biblioteca: " + biblioteca) 
  all_posts = []
  try:
    all_posts = get_posts(biblioteca, pages=300)
  except Exception:
    print(f"Error while requesting Biblioteca: {biblioteca}")
    print("Checking the next one..")
  for post in all_posts:
    post['title'] = biblioteca
    print(post['title'])    
    print(post['post_id'])
    print(post['time'])
    print(post['text'])
    print(post['image'])
    print(post['video'])
    print(post['likes'])
    print(post['comments'])
    print(post['shares'])
    print(post['link'])
     
    data = [post['title'],post['post_id'], post['time'], post['text'], post['image'], post['video'], post['likes'], post['comments'], post['shares'], post['link']]

    with open("funcionaporfavor.csv", "a", newline="", encoding="utf-8") as f:
      writer = csv.writer(f)
      writer.writerow(data)

Mounting the Drive

from google.colab import drive
drive.mount ('/drive')

New Code with df

import facebook_scraper
import pandas as pd
import csv
from facebook_scraper import get_posts

listaBibliotecas = ["bibliotecafoa"]

for biblioteca in listaBibliotecas:
  print("Biblioteca: " + biblioteca) 
  all_posts = []
  try:
    all_posts = get_posts(biblioteca, pages=300)
  except Exception:
    print(f"Error while requesting Biblioteca: {biblioteca}")
    print("Checking the next one..")
  for post in all_posts:
    post['title'] = biblioteca
    print(post['title'])    
    print(post['post_id'])
    print(post['time'])
    print(post['text'])
    print(post['image'])
    print(post['video'])
    print(post['likes'])
    print(post['comments'])
    print(post['shares'])
    print(post['link'])
     
    data = [post['title'],post['post_id'], post['time'], post['text'], post['image'], post['video'], post['likes'], post['comments'], post['shares'], post['link']]
    
    df_fdist.to_csv('/content/drive/My Drive/data.csv')

Error

    ---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-09d5240acb25> in <module>()
     29     data = [post['title'],post['post_id'], post['time'], post['text'], post['image'], post['video'], post['likes'], post['comments'], post['shares'], post['link']]
     30 
---> 31     df_fdist.to_csv('/content/drive/My Drive/data.csv')

NameError: name 'df_fdist' is not defined

Expected outcome

I need to fix this code in order to have the results written on my Google Drive.

Instead of:

df_fdist.to_csv('/content/drive/My Drive/data.csv')

Use:

data.to_csv('/content/drive/My Drive/data.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM