我正在编写一个代码(是的,我是新手)从 facebook 上的页面中提取信息。我正在使用 facebook-scraper 来获取信息。我需要创建一个 CSV 文件来存储这些信息,但我总是空着。
原始代码
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'])
存储在 CSV 文件中的代码。
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))
嘿,非常感谢。现在很有意义。但是,它仍然不起作用,因为它现在只检索一个请求,而不是 10 页。
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))
第三次尝试。仍然只收到一个帖子。
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))
第四次尝试。
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)
退货
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>