Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Facebook from Python

I am trying to access Facebook from Python :D

I want to fetch some data which requires I be logged in in order to view. I know I will require cookies and such to view said data with Python, but I am entirely clueless when it comes to cookies.

How can I use Python to login to Facebook, navigate to multiple pages and retrieve some data?

like image 824
Matthew Avatar asked Apr 12 '26 01:04

Matthew


1 Answers

Okay. Potentially this is a very large question. Instead of using the standard API to retrieve information, you wish to screen scrap?

It's possible - although not recommended as screen scraping is reliant upon the HTML format not changing. However it's not an impossible task.

To get started, you want to look at opening a url:

http://docs.python.org/library/urllib2.html

It's super easy - The example on the page will show you something like this:

>>> import urllib2
>>> f = urllib2.urlopen('http://facebook.com/')
>>> print f.read()

And you see you have HTML.

Now facebook will be smarter than your average site to circumvent this type of login ed: I hope

So you may wan to look at handling the session manually:

import urllib2
req = urllib2.Request('http://www.facebook.com/')
req.add_header('Referer', 'http://www.lastpage.com/')
r = urllib2.urlopen(req)

All snipped from the python docs.

like image 64
Glycerine Avatar answered Apr 13 '26 15:04

Glycerine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!