Created
December 5, 2012 05:35
-
-
Save abelsonlive/4212647 to your computer and use it in GitHub Desktop.
use facepy for facebook feed dumps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?