启动python终端会话失败_使用Python请求的身份验证登录失败,即使在会话中也是如此...

在对this blog post about "Logging in With Requests"和{a2}的代码片段进行了非常好的解释之后,我有以下代码(*)以便进入和浏览具有身份验证的网站:import requests, lxml.html

logurl = 'http://www.somesite.fr/subsite/'

url2 = 'http://www.somesite.fr/subsite/anotherpath/1135'

with requests.session() as s:

login = s.get(logurl)

login_html = lxml.html.fromstring(login.text)

hidden_inputs = login_html.xpath(r'//form//input[@type="hidden"]')

form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}

form['email'] = 'myemail'

form['password'] = 'mypassword'

response = s.post(logurl, data=form)

r2 = s.get(url2)

如果我print form:

^{pr2}$

使用r2 = s.get(url2),我试图在验证后导航到此网站。url2是我登录后“手动”导航时得到的url,这两个页面的html(和外观)完全不同。但是如果我做print response.text和r2.text,我得到的html代码完全相同,即登录页面的代码。我的结论是登录不成功,或者会话没有保持此状态。。。在

我做错什么了?谢谢!在

编辑

运行Brian M.Sheldon建议的代码:import logging

import requests

# enable debug logging with basic logging config

logging.basicConfig(level=logging.DEBUG)

with requests.session() as s:

s.headers['user-agent'] = 'myapp' # use non-default user-agent

response = s.post(logurl, data={'email': 'myemail', 'password': 'mypassword'})

print response.headersDEBUG:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): www.somesite.fr

DEBUG:requests.packages.urllib3.connectionpool:http://www.somesite.fr:80

"POST /subsite/ HTTP/1.1" 200 1415

并且response.headers是:{'Content-Length': '1415', 'Content-Encoding': 'gzip', 'Set-Cookie':

'PHPSESSID=741q7fj6pnkdl1ho4pr6s35cl1; path=/', 'Expires': 'Thu, 19

Nov 1981 08:52:00 GMT', 'Vary': 'Accept-Encoding,Origin',

'Keep-Alive': 'timeout=5, max=100', 'Server': 'Apache', 'Connection':

'Keep-Alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-store,

no-cache, must-revalidate, post-check=0, pre-check=0', 'Date': 'Tue,

25 Apr 2017 14:57:52 GMT', 'Content-Type': 'text/html; charset=UTF-8'}

s.cookies是:RequestsCookieJar[ Cookie PHPSESSID=t9t9gvt7enp70v5mb2viebr8v0 for

www.somsite.fr/ ]>

并且s.get(url2)给出:

^{4磅}$

这有助于理解我做错了什么吗?在

PS:显然,这一领域在过去几年里发展迅速,而几年前的一些答案似乎已经过时/被更好的选择所取代。从我的阅读资料来看,我认为{}是实现我想要的最好方法,但也欢迎其他解决方案。如果我忘了一些有用的信息,请告诉我,我会编辑的。

(*)我很抱歉,我的问题是一个网站与认证,我不能给出一个可复制的例子。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值