Skip to content

Instantly share code, notes, and snippets.

@Henrique-Miranda
Created May 15, 2019 04:15
Show Gist options
  • Save Henrique-Miranda/eaf6940db2a82357579be6c86dd98267 to your computer and use it in GitHub Desktop.
Save Henrique-Miranda/eaf6940db2a82357579be6c86dd98267 to your computer and use it in GitHub Desktop.
Facebook login using python and libs requests, beaultifulsoup 4
import sys
try:
import requests
except:
print('Please install requests: "pip install requests"')
sys.exit()
try:
from bs4 import BeautifulSoup as bs
except:
print('Please install BeautifulSoup: "pip install bs4"')
sys.exit()
if len(sys.argv) < 3:
print("Usage: python3 facelogin.py email password")
sys.exit()
# Faz a primeira request para pegar os dados randômicos do form
url = 'https://m.facebook.com/login.php'
html = requests.get(url).text
HTML = bs(html, 'html.parser')
payload = {}
email = sys.argv[1]
passwd = sys.argv[2]
r = HTML.findAll('input')
for i in r:
payload[i.get('name')] = i.get('value')
payload['email'] = email
payload['pass'] = passwd
payload['login'] = 'Log In'
# print(payload)
# Cria uma session para manter os dados do login.
s = requests.Session()
r = s.post(url, data=payload, allow_redirects=False)
r.raise_for_status()
cookies = r.cookies
# print(cookies)
# Proximas requests será atravéz da session s.get(URL)
home = s.get('https://m.facebook.com', cookies=cookies, allow_redirects=False)
bhtml = bs(home.text, 'html.parser')
#print(bhtml)
# Procura pelo link SAIR para saber se o login foi efetuado
logged = bhtml.find('a', {'id': 'mbasic_logout_button'})
try:
if logged.text:
print(f"Login efetuado, usuário: {logged.text.split('(')[1].replace(')', '')}")
except:
print("Login e/ou senha incorretos")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment