Facebook Scraping using mechanize

Let us begin the fun..

Some Prerequisite:- You must have the basic knowledge of Python.

Note:- If you don't know basic of python then please let me know so that i can write down some tutorials for the basic also.

In this post I am going to show you how to change the password of your friend facebook id without knowing him.

To do this we need to create a scraper for the facebook.
So let us start the scraping facebook. To scrap the facebook we need the mechanize library. It is a fantastic library for the scraping.

Let us run the python interpreter. And after running it.

1st we need to load the module for our script

import mechanize

Next thing we need to do is create a browser so that we can open a link

fb = mechanize.Browser() // this will create the browser for us

Now let us open the link. But before opening the link we have to set the headers and robot handling for the facebook
If you don't know what is header and robot handling then please google it.

let us handle the robots

fb.set_handle_robots(False) // handling the robots problem

fb.addheaders = [('User-agent','Mozilla/11')] // add header for facebook

now let's open the link

fb.open('http://www.facebook.com') // don't forget to add http://

This will open the link for us.After opening the link we have to login with id and password. As we all know that to log in a website we have to fill a form with our site username and password and then we have to submit it. Similar procedure happens here.
To log in the facebook we have to search for the form. Let's search for the form

for form in br.forms():
   print form

The above code will print the all forms present on the facebook index page.

Now let's check which form is of our interest. The output is shown below:-


Index form are shown in red color and our inputs which we have to fill are shown in blue.

The next thing we have to do it that we have to select the form which we want to fill.

fb.select_form(nr =0)       //here nr = 0 stands for number zero which              represents our form number in the index page
By using above commands we select the form to login the website
Now let us fill the form as we can see that email and pass are the field which take the input from the user and give it to the server.

fb.form['email'] = 'login_id'
fb.form['pass'] = 'password'

After filling the form we have to submit the result.

fb.submit() // this command submit the result to server

Submitting is like the same as we click on the Log in button in the normal browser. After submitting it we get the response like given below:-

<response_seek_wrapper at 0x9a94d4c whose wrapped object = <closeable_response at 0x99f14cc whose fp = <socket._fileobject object at 0x99e3a6c>>>

If we get the response like above then we successfully log into the website.
The cool thing about the mechanize is that it can automatically handles the cookie for us. And we just don't need to create a jar for the cookie handling.

I think this section is quite long. So, thats all for this section. In the next section we will see how we can create a script which is automatically going to change the password for us. And after that section we are going to post some data on the facebook wall of all the friends by just one click.

Thanks for reading this post. And sorry for my bad English..;):):D

1 comment: