Last active
December 5, 2023 09:57
-
-
Save juliusmh/438de446007574d9abbb to your computer and use it in GitHub Desktop.
Facebook Login through HTTP using python requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------- | |
# Name: FACEBOOK TEST | |
# Purpose: | |
# | |
# Author: Julius | |
# | |
# Created: 16.06.2015 | |
# Copyright: (c) Julius 2015 | |
# Licence: <APACHE> | |
#------------------------------------------------------------------------------- | |
#IMPORTS | |
import requests | |
from bs4 import BeautifulSoup | |
#CONSTRAINTS | |
EMAIL = "xyz@web.de" | |
PASSW = "abcdefgh" | |
LOGIN_URL = "https://m.facebook.com/login.php?refsrc=https%3A%2F%2Fm.facebook.com%2F&refid=8" | |
FACEBOOK_URL = "https://m.facebook.com/" | |
#VARS | |
s = None | |
#MAIN CLASS | |
class facebook(): | |
def __init__(self): | |
self.s = requests.session() | |
self.login() | |
def login(self): | |
#GET DEFAULT VALUES FROM PAGE | |
r = self.s.get(FACEBOOK_URL) | |
soup = BeautifulSoup(r.text) | |
#GET DEFAULT VALUES | |
tmp = soup.find(attrs={"name": "lsd"}) | |
lsd = tmp.get("value") | |
tmp = soup.find(attrs={"name": "charset_test"}) | |
csettest = tmp.get("value") | |
tmp = soup.find(attrs={"name": "version"}) | |
version = tmp.get("value") | |
tmp = soup.find(attrs={"name": "ajax"}) | |
ajax = tmp.get("value") | |
tmp = soup.find(attrs={"name": "width"}) | |
width = tmp.get("value") | |
tmp = soup.find(attrs={"name": "pxr"}) | |
pxr = tmp.get("value") | |
tmp = soup.find(attrs={"name": "gps"}) | |
gps = tmp.get("value") | |
tmp = soup.find(attrs={"name": "dimensions"}) | |
dimensions = tmp.get("value") | |
tmp = soup.find(attrs={"name": "m_ts"}) | |
m_ts = tmp.get("value") | |
tmp = soup.find(attrs={"name": "li"}) | |
li = tmp.get("value") | |
data = { | |
'lsd': lsd, | |
'charset_test': csettest, | |
'version': version, | |
'ajax': ajax, | |
'width': width, | |
'pxr': pxr, | |
'gps': gps, | |
'm_ts': m_ts, | |
'li': li, | |
} | |
data['email'] = EMAIL | |
data['pass'] = PASSW | |
data['login'] = 'Log In' | |
r = self.s.post(LOGIN_URL , data=data) | |
print r.text | |
fb = facebook() |
Puh i dont know, i just tried some links and that one worked. Propably there are more alternatives..
Code not working. Please update.
It's been 6 years. Probably the login mechanism changed @stark821.
So now is it impossible to login Facebook through Http Requests ?
You can do It, first extract the input forums then input your fields, I did it a long time ago but do not know if it works
from bs4 import BeautifulSoup
def login(email, password):
# Set up session
session = requests.Session()
# Set headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
session.headers.update(headers)
# Get login page
response = session.get('https://www.facebook.com/')
soup = BeautifulSoup(response.content, 'html.parser')
# Extract form data and input fields
form_data = {}
for input_field in soup.find_all('input'):
if input_field.has_attr('name'):
form_data[input_field['name']] = input_field.get('value', '')
# Fill in form data
form_data['email'] = email
form_data['pass'] = password
# Submit form
response = session.post('https://www.facebook.com/login.php', data=form_data)
# Check if login was successful
if 'Find Friends' in response.text:
print('Login successful!')
else:
print('Login failed.')
print(form_data)
Example usage
login('example@gmail.com', 'passcode')
You are a legend bro 🤍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why doesn't the final post request is working on FACEBOOK_URL = "https://m.facebook.com/" but on LOGIN_URL = "https://m.facebook.com/login.php?refsrc=https%3A%2F%2Fm.facebook.com%2F&refid=8" and similar links like it...?