Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created June 3, 2013 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhurliman/5696050 to your computer and use it in GitHub Desktop.
Save jhurliman/5696050 to your computer and use it in GitHub Desktop.
Dump recent Facebook profile info, likes, photos, and posts to JSON text files
import urllib2
import json
FB_TOKEN = '...'
MAX_PAGES = 10
def scrape_page(page):
all_data = []
url = ('https://graph.facebook.com' + page + '?limit=50&access_token=' +
FB_TOKEN)
current_page = 1
while current_page <= MAX_PAGES:
response = urllib2.urlopen(url)
data = response.read()
json_data = json.loads(data)
print 'Fetched ' + page + ' page ' + str(current_page)
current_page += 1
if 'username' in json_data:
return json_data
if 'data' in json_data:
all_data.extend(json_data['data'])
else:
print 'ERROR:'
print json_data
break
if 'paging' in json_data and 'next' in json_data['paging']:
url = json_data['paging']['next']
else:
break
return all_data
def write_data(data, filename):
with open(filename, 'w') as outfile:
json.dump(data, outfile)
write_data(scrape_page('/me'), 'about.txt')
write_data(scrape_page('/me/likes'), 'likes.txt')
write_data(scrape_page('/me/photos'), 'photos.txt')
write_data(scrape_page('/me/posts'), 'posts.txt')
@divyasharma94410
Copy link

I tried it but it's not working. Or does it only works on the profiles those are not locked?

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