It's not showing any errors, but I'm wondering why this code does not successfully log into Facebook. The returned HTML from the response shows that I'm still not logged in. Just wondering if the 'payload' parameters are being properly submitted. If not, what exactly is the problem?
import requests
import re
# Create a session object
session = requests.Session()
# Define the target URL of the form
url = "https://www.facebook.com/login"
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',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
}
# Use the GET method to retrieve the form page
response = session.get(url, headers=headers)
# Parse the HTML content of the form page
html = response.text
# Use a regular expression to extract the payload parameters from the HTML
payload = {}
for match in re.finditer("<input .*?name=[\"'](.*?)[\"'].*?value=[\"'](.*?)[\"'].*?>", html):
payload[match.group(1)] = match.group(2)
email = {'email': 'MY-EMAIL-ADDRESS@GMAIL.COM'}
password = {'pass': 'MY-PASSWORD-GOES-HERE'}
payload.update(email)
payload.update(password)
# Submit the form with the extracted payload parameters
response = session.post(url, headers=headers, data=payload)
# Print the response from the server
print(response.text)