Logging into Facebook with requests Python 2020

Viewed 873

Hello I would like to make a bot that automatically logs into facebook and makes a post on a specific group.I think I will use selenium to make a post, which will be easy so I am just asking for help with the first part. I have problems because some form data from network developer tool tab is hidden and not displayed in websites html and I don't know how to find it. Here is my code so far:

import requests
from bs4 import BeautifulSoup

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}
data = {
    'email': '----------',
    'pass': '---------',
    'timezone': '-60',
    'locale': 'pl_PL',
    'next': 'https://www.facebook.com/',
    'login_source': 'login_bluebar',
    'prefill_contact_point': '512250794',
    'prefill_source': 'browser_onload',
    'prefill_type': 'password',
    'skstamp': 'eyJoYXNoIjoiYThiN2EyOTMwNTJhZTUzODg0YjZiNWNlOWQ1NzZjZjUiLCJoYXNoMiI6IjQ3ZWI4M2U1ZjVmYTQxMTQ4MDIxYWVlZTgzNTk3ZWJmIiwicm91bmRzIjo1LCJzZWVkIjoiYjU0NWE4MzczOTgwYTZhODViZjUzYmE3ZmM0OWIyOWYiLCJzZWVkMiI6IjdiNTU0NzBjM2M5NjlhMTY3YmZkZmIwZjE5ODlmNDdhIiwidGltZV90YWtlbiI6ODA3OTAsInN1cmZhY2UiOiJsb2dpbiJ9'
}
with requests.Session () as s:
    url = 'https://www.facebook.com/'
    r = s.get(url, headers=headers)
    soup = BeautifulSoup(r.content, 'lxml')
    data['jazoest'] = soup.find('input', attrs={'name': 'jazoest'})['value']
    data['lsd'] = soup.find('input', attrs={'name': 'lsd'})['value']
    data['lgnrnd'] = soup.find('input', attrs={'name': 'lgnrnd'})['value']
    data['lgndim'] = soup.find('input', attrs={'name': 'lgndim'})['value']
    data['ab_test_data'] = soup.find('input', attrs={'name': 'ab_test_data'})['value']
    data['lgnjs'] = soup.find('input', attrs={'name': 'lgnjs'})['value']
    data['guid'] = soup.find('input', attrs={'name': 'guid'})['value']
    data['lgndim'] = soup.find('input', attrs={'name': 'lgndim'})['value']

    r = s.post(url, data=data, headers=headers)

    print(r.content)

I would be very happy if someone could help me with it. Is there a better way to do such things in 2020? Yes I know that there were made some posts about logging into facebook with requests with bs4 but they are from 2018 and I think that facebook changed a lot, like some headers disappear or change their name after each time I log in.

1 Answers

You can use the Facebook API, which is available at developers.facebook.com

Rather than using a third-party library, you could post directly to the group using the API(see here for more details)

Related