2016-09-16 279 views
2

我想使用Python登錄到我的Google帳戶,但是當我打印html結果時,它不顯示我的用戶名。這就是我知道它沒有登錄。使用Python登錄Google帳戶?

如何使用Python登錄到谷歌?到目前爲止,我已經看到了兩個受歡迎的模塊,用於這個urllib.request或Requests,但是沒有人幫助我登錄到巨人Google。

代碼:

import requests 

# Fill in your details here to be posted to the login form. 
payload = { 
'Email': '[email protected]', 
'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計劃。

+1

我建議OAuth發送純文本密碼。 http://stackoverflow.com/questions/10271110/python-oauth2-login-with-google –

回答

0

除了使用oAuth或他們的API,谷歌有像captcha等東西,以防止機器人暴力和猜測密碼。

你可以嘗試欺騙用戶代理,但我仍然相信這是靜默。

+0

會像https://pypi.python.org/pypi/fake-useragent結束工作 –

+0

你實際上並不需要一個用戶代理;) –

1

這將讓你登錄:

from bs4 import BeautifulSoup 
import requests 


form_data={'Email': '[email protected]', '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 [email protected],你需要使用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 
+0

我設置變量my_mail =我的電子郵件和輸出返回什麼都沒有 my_mail =「[email protected]」 –

+0

輸出不可能是沒有什麼。根據第一個代碼示例,按照發布的代碼運行代碼,創建form_data字典。 –

+0

檢查這張圖片的代碼,並告訴我,如果你可以發現我做錯了什麼,這將是非常感激http://imgur.com/lDgt8IM –

相關問題