我正尝试使用以下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错误的请求错误
我不知道,我其实没有登录,我不知道如何监控。
如果不是在网络浏览器中,网站可能会阻止我登录吗?
你有看着使用类似硒? FT完全可能阻止自动访问,(尝试在头文件中设置用户代理) – zhqiat
发送登录请求后是否返回任何cookie?我不确定您尝试登录的网站是如何处理身份验证的,但我认为您并未在请求之间维护会话,因此您尝试的第2个GET请求在头中没有经过身份验证的会话Cookie。 –
感谢您的回答。你是对的@艾伦,第二个GET请求是错误的,但是,FT阻止来自浏览器外部的连接。一些JavaScript代码必须在每个登录阶段后运行,甚至使用Qt for python不会围绕... – ylnor