リクエストを使用してウェブサイトにログインしようとしています。pythonリクエストを使用してウェブサイトにログイン:400 - 不正リクエスト
ログインプロセスは、二段階である:
ステップ1:最初のページにメールアドレスを入力して。下記の第一ページのソースコード:
<div id="content" class="js-container" data-component="two-step-login-form"> <div class="lgn-box"> <form name="enter-email-form" action="/login/submitEmail" class="js-email-lookup-form" method="POST" data-test-id="enter-email-form" novalidate="true"> <input name="location" value="https://www.mywebsite.com/" type="hidden"> <input name="continueUrl" value="" type="hidden"> <input name="readerId" value="" type="hidden"> <input name="loginUrl" value="/login?location=https%3A%2F%2Fwww.mywebsite.com%2F" type="hidden"> <div class="lgn-box__title"> <h1 class="lgn-heading--alpha">Sign in</h1> </div> <div class="o-forms-group"> <label for="email" class="o-forms-label">Email address</label> <input id="email" class="o-forms-text js-email" name="email" maxlength="64" autocomplete="off" autofocus="" required="" type="email"> <input id="password" name="password" style="display:none" type="password"> <label for="password"> </label></div> <div class="o-forms-group"> <button class="o-buttons o-buttons--standout o-buttons--big" type="submit" name="Next">Next</button> </div> </form>
ステップ2:返された2番目のページでパスワードを入力します。以下の2ページ目のソースコード:
import requests, lxml.html with requests.Session() as s: login = s.get('https://mywebsite/login') login_html = lxml.html.fromstring(login.text) hidden_inputs = login_html.xpath(r'//form//input[@type="email"]') form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs} form['email'] = '[email protected]' response = s.post('https://mywebsite/login', data=form) >> Bad Request 400 ERROR login_html = lxml.html.fromstring(response.text) hidden_inputs = login_html.xpath(r'//form//input[@type="password"]') form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs} form['password'] = '*****' response = s.post('https://website/login', data=form) >> Bad Request 400 ERROR print(form)
任意のアイデアはどのように処理する:
<div id="content" class="js-container" data-component="two-step-login-form"> <div class="lgn-box"> <form name="enter-password-form" action="/login?location=https%3A%2F%2Fwww.mywebsite.com%2F" method="POST" data-test-id="enter-password-form" novalidate=""> <input name="location" value="https://www.mywebsite.com/" type="hidden"> <input name="continueUrl" value="" type="hidden"> <input name="readerId" value="" type="hidden"> <div class="lgn-box__title"> <h1 class="lgn-heading--alpha">Sign in</h1> </div> <div class="o-forms-group"> <a href="/login?location=https%3A%2F%2Fwww.mywebsite.com%2F" class="js-change-email lgn-typography-bold lgn-typography-big lgn-typography-link--back">Back</a> </div> <div class="o-forms-group"> <label for="email" class="o-forms-label">Email</label> <input readonly="" class="js-email o-forms-text o-forms-unskin lgn-typography-bold lgn-typography-big lgn-typography-truncate" id="email" value="" name="email" type="text"> </div> <div class="o-forms-group"> <label for="password" class="o-forms-label">Password</label> <input id="password" name="password" class="o-forms-text" maxlength="50" autofocus="" required="" type="password"> <small class="o-forms-additional-info"><a href="/reset-password">Forgot your password?</a></small> </div> <div class="o-forms-group lgn-utils-pack"> <button class="o-buttons o-buttons--standout o-buttons--big" type="submit" name="Sign in">Sign in</button> <div class="lgn-typography-align-right"> <input class="o-forms-checkbox" name="rememberMe" id="rememberMe" value="true" checked="" type="checkbox"> <label for="rememberMe" class="o-forms-label lgn-utils-remove-margin">Remain signed in</label> </div> </div> </form>
がそうするために、私は次のコードを使用していますか?
おかげ
(パイソン3.5)
mechanizeを使用しようとしましたか? – Costis94