从网页抓取信息后如何创建Python CSV文件?

时间:2020-11-08 14:09:33

标签: python facebook csv

我正在编写代码(是的,我是新来的),以从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))

第三次尝试。 Stil只收到一个帖子。


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>

1 个答案:

答案 0 :(得分:1)

您的代码有两个问题。

第一个问题是如何创建data

[print(post['post_id']), print(post['text']), print(post['image'])]

为什么

在此行中,当您获取值时就进行打印,打印的return值为None,因此None存储在列表中。

每次迭代时data的旧输出:[None, None, None]

更正:

[post['post_id'], post['text'], post['image']]

新的data的输出:['2092819824183367', 'Biblioteca da Unesp em Bauru ganha nova identidade visual ❤️\n\nhttps://youtu.be/dTCGp1eGmtM\n\nYOUTUBE.COM\nBiblioteca da Unesp em Bauru ganha nova identidade visual', None]

(PS:idk转换为什么内容)

第二个问题是您写入文件的方式。

open("facebook.csv", "w", newline="")

在写入文件时,请注意a中的open("facebook.csv", "a", newline=""),这用于打开文件以在“追加”模式下打开文件,并在w模式下打开文件(您的旧代码)将在每个循环中覆盖文件,从而在每个循环中产生一个新的空白文件,而这种行为不是您所需要的。

因此,将所有更改和缩进集成在一起,这是您所期望的完整代码

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)

关于Unicode错误

打开文件时可以使用open("facebook.csv", "a", newline="",encoding="utf-8")