如何下载所有Facebook页面消息?

时间:2019-09-02 17:00:07

标签: python facebook web-scraping

我想下载我设法帮助建立聊天机器人的Facebook帐户中的所有聊天消息。

我检查了其他帖子,例如Download Facebook Page Private MessagesHow to download all Facebook 'posts to page' (without scrolling),但是它们都不允许从页面下载所有消息。

使用API​​的Facebook请求仅部分返回结果,因此我需要逐页查找所有消息。虽然我可以使用 facebook-api 遍历某些消息,但这种分页的概念使我无法找到文档。

到目前为止,我已经获得了这段代码:

import facebook
import multiprocessing as mp
import requests

app_id = "XXXXX"
token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

graph = facebook.GraphAPI(access_token=token, version = 3.1)
message_dict = {}


#Function to help with the multiprocessing
def get_conversation_from_id(conversation_id):
    conversation = dict()
    conversation[conversation_id] = []
    messages = graph.request(conversation_id + "?fields=messages{" + conversation_id + "}")
    for message_data in messages['messages']['data']:
        try:
            message = graph.request(message_data['id'] + "?fields=message")
            conversation[conversation_id].append(message['message'])
        except facebook.GraphAPIError:
            continue
    conversation[conversation_id] = conversation[conversation_id][::-1]
    return conversation

conversations = graph.request(app_id + "?fields=conversations{0}&amp")

#A while loop that is to be broken once no more "next" pages are found
while True:
    try:
        pool = mp.Pool(10)
        results = pool.map(get_conversation_from_id, [conv_id['id'] for conv_id in conversations['conversations']['data']])
        print(results)
        pool.close()
        conversations = graph.request(app_id + "?fields=conversations{" + str(i) + "}&limit=100")
    except requests.exceptions.ConnectionError:
        continue

我希望能够在所有页面上分页(我什至在请求中找不到试图分页到另一个答案子集的信息),然后将消息从中保存到Json进行进一步处理。

0 个答案:

没有答案