Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Created December 5, 2012 05:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsonlive/4212647 to your computer and use it in GitHub Desktop.
Save abelsonlive/4212647 to your computer and use it in GitHub Desktop.
use facepy for facebook feed dumps
from facepy import GraphAPI
import facepy
import re
import json
#meta variables
access_token = 'your_token'
page_id = 'the_page' # input page id here
base_query = page_id + '/feed?limit=300'
# scrape the first page
print 'scraping:', base_query
graph = GraphAPI(access_token)
feed = graph.get(base_query)
data = feed['data']
# determine the next page
next = feed['paging']['next']
next_search = re.search('.*(\&until=[0-9]+)', next, re.IGNORECASE)
if next_search:
the_until_arg = next_search.group(1)
# scrape the rest of the pages
while (next is not False):
the_query = base_query + the_until_arg
print 'baking:', the_query
try:
feed = graph.get(the_query)
data.append(feed['data'])
except facepy.exceptions.OAuthError:
print 'start again at', the_query
break
# determine the next page, until there isn't one
try:
next = feed['paging']['next']
next_search = re.search('.*(\&until=[0-9]+)', next, re.IGNORECASE)
if next_search:
the_until_arg = next_search.group(1)
except IndexError:
print 'last page...'
next = False
total_scraped = total_scraped + 300
print total_scraped, 'pies in the face so far'
# dump to json
with open('data.json', 'wb') as fp:
json.dump(data, fp)
@LSMatos
Copy link

LSMatos commented Oct 21, 2016

Hello, I'm starting in the area of programming and facepy. I would like to ask you how do I stop the process and generate JSON file? I know there is the stretch at the end of the code for this, but when I ran here he was looking for all the feed page and did not stop. If I want to stop before and generate and view this file, how do you proceed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment