简体   繁体   English

Python 请求 - 会话未捕获响应 cookie

[英]Python requests - Session not capturing response cookies

I'm not sure how else to describe this.我不知道如何描述这一点。 I'm trying to log into a website using the requests library with Python but it doesn't seem to be capturing all cookies from when I login and subsequent requests to the site go back to the login page.我正在尝试使用 Python 的请求库登录网站,但它似乎没有从我登录时捕获所有 cookie,随后对该站点的请求返回登录页面。

The code I'm using is as follows: (with redactions)我使用的代码如下:(有删节)

with requests.Session() as s:
    r = s.post('https://www.website.co.uk/login', data={
        'amember_login': 'username',
        'amember_password': 'password'
    })

Looking at the developer tools in Chrome.查看 Chrome 中的开发人员工具。 I see the following:我看到以下内容: 在此处输入图片说明

After checking r.cookies it seems only that PHPSESSID was captured there's no sign of the amember_nr cookie.检查r.cookies ,似乎只有 PHPSESSID 被捕获,没有 amember_nr cookie 的迹象。

The value in PyCharm only shows: PyCharm 中的值仅显示:

{RequestsCookieJar: 1}<RequestsCookieJar[<Cookie PHPSESSID=kjlb0a33jm65o1sjh25ahb23j4 for .website.co.uk/>]>

Why does this code fail to save 'amember_nr' and is there any way to retrieve it?为什么此代码无法保存 'amember_nr' 并且有什么方法可以检索它?

SOLUTION :解决方案

It appears the only way I can get this code to work properly is using Selenium, selecting the elements on the page and automating the typing/clicking.看来我能让这段代码正常工作的唯一方法是使用 Selenium,选择页面上的元素并自动输入/点击。 The following code produces the desired result.以下代码产生所需的结果。

from seleniumrequests import Chrome

driver = Chrome()
driver.get('http://www.website.co.uk')
username = driver.find_element_by_xpath("//input[@name='amember_login']")
password = driver.find_element_by_xpath("//input[@name='amember_pass']")

username.send_keys("username")
password.send_keys("password")

driver.find_element_by_xpath("//input[@type='submit']").click() # Page is logged in and all relevant cookies saved.

You can try this:你可以试试这个:

with requests.Session() as s:
    s.get('https://www.website.co.uk/login')
    r = s.post('https://www.website.co.uk/login', data={
        'amember_login': 'username',
        'amember_password': 'password'
    })

The get request will set the required cookies. get 请求将设置所需的 cookie。

FYI I would use something like BurpSuite to capture ALL the data being sent to the server and sort out what headers etc are required ... sometimes people/servers to referrer checking, set cookies via JAVA or wonky scripting, even seen java obfuscation and blocking of agent tags not in whitelist etc... it's likely something the headers that the server is missing to give you the cookie.仅供参考,我会使用 BurpSuite 之类的东西来捕获发送到服务器的所有数据,并找出需要哪些标头等......有时人们/服务器要检查引用,通过 JAVA 或奇怪的脚本设置 cookie,甚至看到 Java 混淆和阻塞不在白名单中的代理标签等等......这可能是服务器缺少的标头为您提供cookie。

Also you can have Python use burp as a proxy so you can see exactly what gets sent to the server and the response.你也可以让 Python 使用 burp 作为代理,这样你就可以准确地看到发送到服务器的内容和响应。

https://github.com/freeload101/Python/blob/master/CS_HIDE/CS_HIDE.py (proxy support ) https://github.com/freeload101/Python/blob/master/CS_HIDE/CS_HIDE.py (代理支持)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM