2017-04-06 151 views
0

我正嘗試使用以下python代碼登錄到我的帳戶,但未成功。登錄過程分兩個頁面進行。首先輸入登錄名,然後輸入密碼。我使用Python3:Python:嘗試使用請求登錄並執行HTTP請求

from bs4 import BeautifulSoup 
import requests, lxml.html 

with requests.Session() as s: 
    #First login page 
    login = s.get('https://accounts.ft.com/login') 
    login_html = lxml.html.fromstring(login.text) 
#getting the form inputs 
    hidden_inputs = login_html.xpath(r'//form//input') 
    form = {x.name: x.value for x in hidden_inputs} 
#filling inputs with email 
    form['email'] = '[email protected]' 
    response = s.post('https://accounts.ft.com/login', data=form) 
# Receive reponse 200 

#Second login page 
    login_html = lxml.html.fromstring(response.text) 
#getting inputs 
    hidden_inputs = login_html.xpath(r'//form//input') 
    form = {x.name: x.value for x in hidden_inputs} 
#filling inputs with email and password 
    form['email'] = '[email protected]' 
    form['password'] = 'p****word' 
    response = s.post('https://accounts.ft.com/login', data=form) 
#Receive reponse 200 

#Trying to read an article being loggedIn 
    page = s.get('https://www.ft.com/content/173695cc-1a98-11e7-a266-12672483791a') 
    soup = BeautifulSoup(page.content, 'html.parser') 
    print(soup.prettify()) 
# data-next-is-logged-in="false" => Please Register to read this page... 
  • 這裏是表的樣子:

<div class="js-container" data-component="two-step-login-form" id="content"> 
 
    <div class="lgn-box"> 
 
    <form action="/login/submitEmail" class="js-email-lookup-form" data-test-id="enter-email-form" method="POST" name="enter-email-form" novalidate=""> 
 
     <input name="location" type="hidden" value="" /> 
 
     <input name="continueUrl" type="hidden" value="" /> 
 
     <input name="readerId" type="hidden" value="" /> 
 
     <input name="loginUrl" type="hidden" value="/login" /> 
 
     <div class="lgn-box__title"> 
 
     <h1 class="lgn-heading--alpha"> 
 
      Sign in 
 
     </h1> 
 
     </div> 
 
     <div class="o-forms-group"> 
 
     <label class="o-forms-label" for="email"> 
 
     Email address 
 
     </label> 
 
     <input autocomplete="off" autofocus="" class="o-forms-text js-email" id="email" maxlength="64" name="email" required="" type="email"> 
 
     <input id="password" name="password" style="display:none" type="password"> 
 
     <label for="password"> 
 
     </label> 
 
     </input> 
 
     </input> 
 
     </div> 
 
     <div class="o-forms-group"> 
 
     <button class="o-buttons o-buttons--standout o-buttons--big" name="Next" type="submit"> 
 
     Next 
 
     </button> 
 
     </div> 
 
    </form> 
 
    </div>

  • 這裏是傳遞給POST我的數據看起來像:

    表格 {'password':'p **** word','continueUrl':'','loginUrl':'/ login','email':'[email protected]', 'readerId': '', '位置': '兩個第一和第二頁洛200響應'}

  • POST請求的回報。但似乎我還沒有登錄。

  • 我一直在使用http://accounts.ft.com/sso/[email protected]爲POST請求的URL嘗試,返回405錯誤的請求錯誤

  • 我不知道,我其實沒有登錄,我不知道如何監控。

  • 如果不是在網絡瀏覽器中,網站可能會阻止我登錄嗎?

+0

你有看着使用類似硒? FT完全可能阻止自動訪問,(嘗試在頭文件中設置用戶代理) – zhqiat

+0

發送登錄請求後是否返回任何cookie?我不確定您嘗試登錄的網站是如何處理身份驗證的,但我認爲您並未在請求之間維護會話,因此您嘗試的第2個GET請求在頭中沒有經過身份驗證的會話Cookie。 –

+0

感謝您的回答。你是對的@艾倫,第二個GET請求是錯誤的,但是,FT阻止來自瀏覽器外部的連接。一些JavaScript代碼必須在每個登錄階段後運行,甚至使用Qt for python不會圍繞... – ylnor

回答

1

嘗試使用selenium來模擬Web瀏覽器,因爲看起來FT阻止了自動訪問。

或者,您可以查看某個網站是否已被存檔,例如archive.is(這會將大多數網站拉到更加機器友好的設置中)。

最後,既是數據挖掘API和標題API的FT提供他們developer page