0
from bs4 import BeautifulSoup
import json, requests
import lxml.html
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
form_data = {}
url = 'https://accounts.google.com/Login'
login = requests.get(url)
html = lxml.html.fromstring ( login.text )
data = html.xpath('//input[@type="hidden"]')

for x in data:
    name = x.get('name')
    value = x.get('value')
    form_data[name] = value

form_data['Email'] = 'my email here'
form_data['Passwd'] = 'my password here'
print(form_data)

with requests.session() as s:
    response = requests.post(url, data=form_data, headers=headers)
    req = requests.get('https://mail.google.com/mail/u/0/#inbox')

    print(req.text)
    print ( 'inbakke' in req )

I tried making a script that would request a login with post. But it doesnt work. When I look at the "req.text" html at the end. It seems like I am still on the submit your passwd and email page. https://gyazo.com/2db56030077c2e9a73154de0ab9f801c - the "req.text"

The form_data I am sending is the following:

{'_utf8': '☃', 'Email': 'MY EMAIL', 'SessionState': '', 'bgresponse': 'js_disabled', 'GALX': '0vyTLpgavJc', 'ProfileInformation': '', 'gxf': 'AFoagUXHK7rZNkBVVIUkhqVxSRX27RFOLg:1482790613384', 'Page': 'PasswordSeparationSignIn', 'Passwd': 'MY PASSOWRD'}

Can some smart person please explain what went wrong, and how I should fix it?

  • this isn't a documented authentication/email-pulling workflow and it it unfaithful to what goes on with a browsers. pycurl might claim to pull gmail. with browser type workflow, 'requests.get' would be reusing session data in 'response'. – Marc Condon Dec 26 '16 at 22:30
  • I am sorry @MarcCondon I didn't get that, what is the problem? –  Dec 26 '16 at 22:38
  • 1
    use `smtplib` or `imaplib` to work with email. ie. [how-to-send-an-email-with-gmail-as-provider-using-python](http://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python) – furas Dec 26 '16 at 23:03
  • I am not trying to send an email, im trying to log into gmail. Not just gmail, but any other website for that sake. Can smtplib or imaplib help me with that? –  Dec 27 '16 at 17:26
  • If you are trying to log into email servers, then yes `smtplib` will work but it can't help you to log into web applications. – krishh Dec 28 '16 at 14:43
  • for your question, just make sure those are the only fields being posted while login. I observed one more field `{'signIn':'Sign in'}` while I try to login. – krishh Dec 28 '16 at 14:55

0 Answers0