0

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' : 'andrew.whiteman77@gmail.com',
    '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!!

Abhay Salvi
  • 890
  • 3
  • 15
  • 39
  • 2
    possible duplicate of: https://stackoverflow.com/a/21930636/7473057 – Nick H Jan 21 '20 at 16:46
  • 2
    But it's actually from where I copied the code from, and nothing is happening... @Nick Hale –  Jan 21 '20 at 17:09
  • Is there a reason you are using requests? From some reading and a few quick tests, there are a lot easier methods out there, like `mechanize` – Nick H Jan 23 '20 at 08:46
  • Please give me the code of `mechanize` then. I have tried it but it always gives me an error. Any help from you with any library will be helpful... @Nick Hale – Abhay Salvi Jan 23 '20 at 12:56

1 Answers1

1

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('user@user.com', 'password')

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

In [5]: f.response                                                                                                                                                                                                                              
Out[5]: <Response [200]>
Nick H
  • 1,081
  • 8
  • 13
  • It's giving me an error however @Nick Hale, It is asking me to add `features="lxml"` – Abhay Salvi Jan 29 '20 at 08:58
  • 1
    do you have lxml installed? `pip install lxml`. `lxml` is a parser, required to view certain pages – Nick H Jan 29 '20 at 09:03
  • How to implement to my code @Nick Hale ?? (I am the same guy with a different account so I have the same problem. Features = "lxml") –  Jan 30 '20 at 15:36