python - 使用 Python 登录 Google 帐户?

标签 python python-3.x

我想使用 Python 登录我的 Google 帐户,但是当我打印 html 结果时,它没有显示我的用户名。这就是我知道它没有登录的方式。

如何使用 Python 登录谷歌?到目前为止,我已经看到这个 urllib.request 或 Requests 的两个流行模块,但没有一个能帮助我登录到巨大的 Google。

代码:

import requests

# Fill in your details here to be posted to the login form.
payload = {
'Email': 'accountemail@gmail.com',
'Passwd': 'accountemailpassword'
}

# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
p = s.post('https://accounts.google.com/signin/challenge/sl/password', data=payload)
# print the html returned or something more intelligent to see if it's a successful login page.
print(p.text)

登录表单信息:

<input id="Email" name="Email" placeholder="Enter your email" type="email" value="" spellcheck="false" autofocus="">

<input id="Passwd" name="Passwd" type="password" placeholder="Password" class="">

<input id="signIn" name="signIn" class="rc-button rc-button-submit" type="submit" value="Sign in">

当我登录时,控制台会给我 4 个请求链接,所以我不确定我是否使用了正确的 URL。

Request URL:https://accounts.google.com/signin/challenge/sl/password
Request Method:POST
Status Code:302 

Request URL:https://accounts.google.com/CheckCookie?hl=en&checkedDomains=youtube&checkConnection=youtube%3A503%3A1&pstMsg=1&chtml=LoginDoneHtml&service=youtube&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&gidl=CAASAggA
Request Method:GET
Status Code:302 

Request URL:https://accounts.google.com/CheckCookie?hl=en&checkedDomains=youtube&checkConnection=youtube%3A503%3A1&pstMsg=1&chtml=LoginDoneHtml&service=youtube&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&gidl=CAASAggA
Request Method:GET
Status Code:302 

request URL:https://www.youtube.com/signin?hl=en&feature=sign_in_button&app=desktop&action_handle_signin=true&next=%2F&auth=xAMUT-baNWvXgWyGYfiQEoYLmGv4RL0ZTB-KgGa8uacdJeruODeKVoxZWwyfd-NezfxB6g.
Request Method:GET
Status Code:303 

我目前正在使用 Python 3.4.2,不打算使用谷歌的 API。

最佳答案

这将使您登录:

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

如果您保存并在浏览器中打开 html,您将看到 Loading you@gmail.com...,您需要 Javascript 才能实际加载页面。您可以通过输入错误的密码进一步验证,如果您这样做,您将再次看到登录页面的 html。

您可以在浏览器中看到发布的内容比您提供的内容多得多,这些值包含在 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">

我显然不会分享我的电子邮件或密码,但你可以将我的电子邮件存储在下面的变量 my_mail 中,你可以在我们测试它时看到它在那里:

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

关于python - 使用 Python 登录 Google 帐户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39540479/

相关文章:

python - 如何在 GTK 中更新绘图区域

python - docker-compose启动错误无法导入名称_thread

python - 替换嵌套循环

python - 说了Python怎么开始录音?

Python run_in_executor 忘记了?

python - 为什么 re.sub 替换整个模式,而不仅仅是其中的一个捕获组?

python pytest 用于测试请求和响应

python - 使用配置文件登录 Python - 通过代码使用文件中定义的处理程序

python - 在 Python 中以编程方式生成视频或动画 GIF?

python - 如何使 tkinter 窗口变圆?