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 = \'
First you need to know the data to be posted. Follow this link.
After you get all the required data the code is simple as follows:
import requests, bs4`
s = requests.Session()
url = 'https://www.facebook.com/login'
res = s.get(url)
form_data = {
# Copy paste the form data here as a valid python dict
}
s.post(url, data=form_data)
# Now try accessing your profile from sessions object
This worked for me.
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
I was also searching for answer. Doing it with requests is pain. So, i used mechanize.
import mechanize
browser = mechanize.Browser()
browser.set_handle_robots(False)
cookies = mechanize.CookieJar()
browser.set_cookiejar(cookies)
browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7')]
browser.set_handle_refresh(False)
url = 'http://www.facebook.com/login.php'
browser.open(url)
browser.select_form(nr = 0) #This is login-password form -> nr = number = 0
browser.form['email'] = YourLogin
browser.form['pass'] = YourPassw
response = browser.submit()
print response.read()
It works. mechanize.browser is emulated browser, so you don't need to send all form values. It will send them as normal browser, you should provide only login and password.
Good luck!