Export Facebook Comments Into A Spreadsheet

Blog

How to export Facebook comments from a post, for free

Recently I was running a contest on Facebook and I needed to export the comments in order to randomly choose a winner. I always keep things fair and use tools to pick random winners for me, but this particular Facebook post had over 3000 of them. All of the free tools online have limits on the number of comments they’ll export, so I had to look for another option.

After some Googling, I ended up in r/DataHoarder on reddit where someone shared some helpful JavaScript to do exactly what I was looking for! It worked and everything went smoothly. I’m sharing this information here on a blog post just for my own future reference, and in case anyone else can benefit from it too.

What To Do

I have a method I’ve used for other purposes that I can share that SHOULD work. It may require too much of a techie approach for most people. If I have access to the comments, I could easily do it. The thing is, you do still have to load all the comments in the browser. I’ll tell you what I’d do and maybe that would help. I recommend doing this on Google Chrome on a computer, rather than a phone/tablet, as it requires you to use the Developer Console.

  1. Click on the date of the post to load only that post on a page. You then have to tell Facebook to show you “All Comments” (rather than Most Recent):
Select "all comments" from the dropdown
  1. When you change that, if there are many (I think more than 50, Facebook will still only show you a certain number), scroll down in the comments box and click the “View more comments” or whatever, so keep clicking this until it disappears, meaning all comments are available:
Keep clicking "View comments" to load everything from the post
  1. When all the comments are loading in the browser (not necessarily the screen, but you could only scroll to see them), press F12, which will show the Developer Console.
Press F12 to load developer console, select "Console"

You can also get to this by right-clicking an element and selecting “Inspect Element”

Inspect element, load developer console, click "Console"

and then selecting the “Console” tab at the top of the Developer Console

In the developer console you'll need to type "allow pasting" before it'll allow you to paste in the JavaScript
  1. Paste in the following code in the code box:

    Note: you may need to type “allow pasting” before the browser will allow you to paste in the JavaScript
(function () { 
    var comments = '';
    [].forEach.call(document.querySelectorAll('.x1n2onr6 span div[dir=auto]'), function(el) {
        var line = el.parentNode.parentNode.parentNode.parentNode.innerHTML;
        line = line.replace(/(<a([^>]+)?>)/ig, '');
        line = line.replace(/(<([^>]+)>)/ig, '{}');
        while (line.indexOf("{}{}") > -1) {
            line = line.replace(/\{\}\{\}/ig, '{}');
        }
        line = line.replace(/^\{\}/ig, "");
        line = line.replace(/\{\}$/ig, "");
        line = line.replace(/\{\}/ig, ": ");
        line = line.replace(/(\{\}Verified account\{\}\s)/ig, "");      
        if (line.length == 0 || line == "{}") return;
        comments += line + "\r\n";
    })
    console.log(comments);
})();
What it looks like with the JavaScript pasted into the console

Press enter.

This is javascript code that will loop through elements with comments and output something similar to this in the developer console, which can be copy/pasted to another program. It may require some cleanup, but it should be helpful. I hope!

The blurred out sections in the console in this screenshot will be the names and comments of the people:

The comments will be exported into one large block of text. There is a "copy" button at the bottom.

What To Do Next

From this point, I personally copy/pasted the data into notepad for some editing. I removed the unnecessary text that it captured from the post, so I just had the various comments separated as lines:

Random comments pasted into notepad

After I had edited, I copy/pasted everything into a Google spreadsheet.

Random comments pasted into a Google spreadsheet, each on it's own line

Each of the lines were pasted into their own separate row, which includes a line number. From this point I went to Random.org and entered in the first/last numbers into their true random number generator. The number it returned is the row of whomever won the contest.

Random.org true random number generator

A little cumbersome, but, free and easy to do.

It’s always been a big deal to me that contests are run with integrity and contest winners are totally random. No favourites, no BS.


Comments


Leave a Comment