I\'m trying to find a way to automatically login to Facebook without browser using Python. I experimented with \"requests\" lib. Tried several ways:
URL = \'
Here's my working Code (May 2017 Python 3.6). To make it work for you, just hard code your own USERNAME, PASSWORD and PROTECTED_URL
# https://gist.github.com/UndergroundLabs/fad38205068ffb904685
# this github example said tokens are also necessary, but I found
# they were not needed
import requests
USERNAME = '-----@yahoo.com'
PASSWORD = '----password'
PROTECTED_URL = 'https://m.facebook.com/groups/318395378171876?view=members'
# my original intentions were to scrape data from the group page
# PROTECTED_URL = 'https://www.facebook.com/groups/318395378171876/members/'
# but the only working login code I found needs to use m.facebook URLs
# which can be found by logging into https://m.facebook.com/login/ and
# going to the the protected page the same way you would on a desktop
def login(session, email, password):
'''
Attempt to login to Facebook. Returns cookies given to a user
after they successfully log in.
'''
# Attempt to login to Facebook
response = session.post('https://m.facebook.com/login.php', data={
'email': email,
'pass': password
}, allow_redirects=False)
assert response.status_code == 302
assert 'c_user' in response.cookies
return response.cookies
if __name__ == "__main__":
session = requests.session()
cookies = login(session, USERNAME, PASSWORD)
response = session.get(PROTECTED_URL, cookies=cookies,
allow_redirects=False)
assert response.text.find('Home') != -1
# to visually see if you got into the protected page, I recomend copying
# the value of response.text, pasting it in the HTML input field of
# http://codebeautify.org/htmlviewer/ and hitting the run button