Tuesday, January 3, 2012

Python Script to fetch the facebook profile picture

Here is the code

# load a given picture from a web page and save it to a file
# (you have to be on the internet to do this)
# tested with Python24     vegaseat     19sep2006

import urllib2
import webbrowser
import os
import sys

# find yourself a picture on a web page you like
# (right click on the picture, look under properties and copy the address)
id = sys.argv[1];
picture_page = "https://graph.facebook.com/"+id+"/picture?type=large"

#webbrowser.open(picture_page)  # test

# open the web page picture and read it into a variable
opener1 = urllib2.build_opener()
page1 = opener1.open(picture_page)
my_picture = page1.read()

# open file for binary write and save picture
# picture_page[-4:] extracts extension eg. .gif
# (most image file extensions have three letters, otherwise modify)
filename = "my_image" + picture_page[-4:]
print filename  # test
fout = open(filename, "wb")
fout.write(my_picture)
fout.close()

# was it saved correctly?
# test it out ...
webbrowser.open(filename)

# or ...
# on Windows this will display the image in the default viewer
#os.startfile(filename)


How to run:
1. download the code and save it as picture.py
2. open the terminal and naviage to the folder containing the file picture.py
3. execute the command
    python picture.py zuck
    where "zuck" is the username of Mark Zuckerberg.

Cheers!!!!

No comments:

Post a Comment