I am writing a code (yes, I am new with this) to extract information from a page on facebook. I am using facebook-scraper to get the info. I need to create a CSV file to store this information, but I always come out empty.
Original code
from facebook_scraper import get_posts
for post in get_posts('bibliotecaunespbauru', pages=66):
print(post['time']) # não funciona
print(post['post_id'])
print(post['text'])
print(post['image'])
print(post['video'])
print(post['likes'])
print(post['comments'])
print(post['shares'])
print(post['link'])
Code to store in the CSV file.
import csv
from facebook_scraper import get_posts
for post in get_posts('bibliotecaunespbauru', pages=10):
data = [print(post['post_id']), print(post['text']), print(post['image'])]
with open("facebook.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(data)
with open('facebook.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
Hey, many thanks for this. it makes so much sense now. However, it still doesn't work as now it retrieves only one request, instead of 10 pages.
import csv
from facebook_scraper import get_posts
for post in get_posts('bibliotecaunespbauru', pages=10):
data = [post['post_id'], post['text'], post['image']]
with open("facebook.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow(data)
with open('facebook.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
Third attempt. Stil getting just one post.
import csv
from facebook_scraper import get_posts
for post in get_posts('bibliotecaunespbauru', pages=10):
data = [post['post_id'], post['text'], post['image']]
with open("facebook.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow(data)
with open('facebook.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ')
for row in data:
print(', '.join(row))
Fourth attempt.
import csv
from facebook_scraper import get_posts
for post in get_posts('bibliotecaunespbauru', pages=10):
data = [post['post_id'], post['text'], post['image']]
with open("facebook.csv", "a", newline="") as f:
writer = csv.writer(f)
writer.writerow(data)
Returns
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-46-b4f7f9df1e02> in <module>
5 with open("facebook.csv", "a", newline="") as f:
6 writer = csv.writer(f)
----> 7 writer.writerow(data)
~\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py in encode(self, input, final)
17 class IncrementalEncoder(codecs.IncrementalEncoder):
18 def encode(self, input, final=False):
---> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
20
21 class IncrementalDecoder(codecs.IncrementalDecoder):
UnicodeEncodeError: 'charmap' codec can't encode characters in position 76-77: character maps to <undefined>