Log into Google account using Python?

后端 未结 2 1883
轻奢々
轻奢々 2020-12-10 09:20

I want to Sign into my Google account using Python but when I print the html results it doesn\'t show my username. That\'s how I know it isn\'t logged in.

How do I s

相关标签:
2条回答
  • 2020-12-10 10:08

    Except by using oAuth or their API, google has things like captcha and so to prevent bots from brute-forcing and guessing passwords.

    You can try and trick the user-agent but I still believe it's to vein.

    0 讨论(0)
  • 2020-12-10 10:23

    This will get you logged in:

    from bs4 import BeautifulSoup
    import requests
    
    
    form_data={'Email': 'you@gmail.com', 'Passwd': 'your_password'}
    post = "https://accounts.google.com/signin/challenge/sl/password"
    
    with requests.Session() as s:
        soup = BeautifulSoup(s.get("https://mail.google.com").text)
        for inp in soup.select("#gaia_loginform input[name]"):
            if inp["name"] not in form_data:
                form_data[inp["name"]] = inp["value"]
        s.post(post, form_data)
        html = s.get("https://mail.google.com/mail/u/0/#inbox").content
    

    If you save and open the html in a browser, you will see the Loading you@gmail.com…, you would need Javascript to actually load the page. You can further verify by putting in a bad password, if you do you will see the html of the login page again.

    You can see in your browser a lot more gets posted than you have provided, the values are contained in the gaia_loginform.

    <form novalidate method="post" action="https://accounts.google.com/signin/challenge/sl/password" id="gaia_loginform">
      <input name="Page" type="hidden" value="RememberedSignIn">
      <input type="hidden" name="GALX" value="5r_aVZgnIGo">
      <input type="hidden" name="gxf" value="AFoagUUk33ARYpIRJqwrADAIgtChEXMHUA:33244249">
      <input type="hidden" id="_utf8" name="_utf8" value="&#9731;"/>
      <input type="hidden" name="bgresponse" id="bgresponse" value="js_disabled">
      <input type="hidden" id="pstMsg" name="pstMsg" value="0">
      <input type="hidden" id="dnConn" name="dnConn" value="">
      <input type="hidden" id="checkConnection" name="checkConnection" value="">
      <input type="hidden" id="checkedDomains" name="checkedDomains"
             value="youtube">
    

    I am obviously not going to share my email or password but you can I have my email stored in a variable my_mail below, you can see when we test for it that it is there:

    In [3]: from bs4 import BeautifulSoup
    
    In [4]: import requests
    
    In [5]: post = "https://accounts.google.com/signin/challenge/sl/password"
    
    In [6]: with requests.Session() as s:
       ...:         soup = BeautifulSoup(s.get("https://accounts.google.com/ServiceLogin?elo=1").text, "html.parser")
       ...:         for inp in soup.select("#gaia_loginform input[name]"):
       ...:             if inp["name"] not in form_data:
       ...:                     form_data[inp["name"]] = inp["value"]
       ...:         s.post(post, form_data)
       ...:         
    
    In [7]: my_mail in  s.get("https://mail.google.com/mail/u/0/#inbox").text
    Out[7]: True
    
    0 讨论(0)
提交回复
热议问题