0
I’m writing a code in Python to scrape information off Facebook. I would like to save this information in a file on Google Drive, since I am working with other people and we use Colaboratory.
Problem
The code does not write the file in Google Colab.
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)
for post in get_posts(biblioteca, pages=300):
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=pd.DataFrame(data)
with open("RedesBibliotecas.csv", "a", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(data)
from google.colab import drive
drive.mount ('/drive')
df.to.csv('/drive/My Drive/Colab Notebooks');
What I tried to
I followed the Colab tutorial but it didn’t work. I already set up the drive with the code:
from google.colab import drive
drive.mount ('/drive')
df.to.csv('/drive/My Drive/Colab Notebooks');
Someone knows how to fix this?
What says the error message?
– Lucas
that
Colab Notebooks
is written just like that. With space? If yes, try saving after space removal– Lucas
@Lucas hi!! Has no error message, just does not write the file in csv. The program runs normal.
– Clara Mendes
@Lucas Hey, thank you for answering. It’s with space yes, both My Drive and Google Colab. At least that’s how it is in Colab’s own tutorial.
– Clara Mendes
Do you have access to the google colab terminal? If so, make sure you can transfer your session files to Drive using the command
gsutil
. Leai sobre aquui: https://cloud.google.com/storage/docs/gsutil In the latter case, save the file to Google Storage and send it to Drive from there– Lucas
Hi Lucas! Thanks for the reading option. I checked the connection and there is nothing wrong, the problem is in the code, I think.
– Clara Mendes
Clara, possibly you don’t have much success with programming - when I answered I noticed some details like exchange "_" for "." , etc -- a computer program nay it will work if we exchange things like this -the names and the syntax has no ambiguity or space to write things "a little different" - the cool may be to interact with Python in interactive mode, with small examples, for example, create a list, insert an element, address an element in the list.You can use colab even for this, in a cell with a few lines of code - d 1 to 5 and have it executed
– jsbueno