Abhay Salvi
Abhay Salvi

Reputation: 1129

Unable to log in to Facebook using Requests lib

I want to log in to Facebook, but it is showing me nothing in the command window. I think it is not working. Any corrections??

Here is my code:

import requests 

s = requests.session()

url1 = "https://m.facebook.com/"

payload = {
    'lsd': 'AVqAE5Wf',
    'charset_test': '€,´,€,´,水,Д,Є', 
    'version': 1,
    'ajax': 0,
    'width': 0,
    'pxr': 0,
    'gps': 0,
    'm_ts': 1392974963,
    'li': 'cxwHUxatQiaLv1nZEYPp0aTB',
    'email' : '[email protected]',
    'pass' : 'xxxxxx',
    'login' : 'Log In',
}


r = requests.post(url1,data=payload)
if 'Find Friends' in r.text or 'Two-factor authentication required' in r.text:
    print("Password found: "+ passw)

Any help would be appreciated!!

Upvotes: 0

Views: 592

Answers (1)

Nick H
Nick H

Reputation: 1079

Ok - after some testing, the best result was with Robobrowser. So pip install robobrowser.

import robobrowser

class Facebook(robobrowser.RoboBrowser):

    url = 'https://facebook.com'

    def __init__(self, email, password):
        self.email = email
        self.password = password
        super().__init__()
        self.login()

    def login(self):
        self.open(self.url)    
        login_form = self.get_form(id='login_form')
        login_form['email'] = self.email
        login_form['pass'] = self.password
        self.submit_form(login_form)

Then, instantiate like this:

f = Facebook('[email protected]', 'password')

Then you can access all the methods from there, I finally ended up with:

In [5]: f.response                                                                                                                                                                                                                              
Out[5]: <Response [200]>

Upvotes: 0

Related Questions