【问题标题】:Python read Cookies from response?Python从响应中读取Cookie?
【发布时间】:2021-04-22 02:35:11
【问题描述】:

在 python 中我有:

cookies = dict(PHPSESSID='PHPSESSID=djsgkjhsdjkhj34',
               authchallenge='sdifhshdfiuh34234234',
               rishum='skdhfuihuisdhf-' + '10403111')

try:
    response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

但我希望仅在第一次使用 cookie 值,然后使用服务器设置的新值,我该怎么做?

我发现的解决方案不使用默认 cookie 进行首次请求。

注意:我无法自动登录该网站,因为它使用身份验证挑战,所以每次我手动登录并仅针对第一次请求更改这些 cookie,然后当服务器更新它们时我想查看这会影响我当前的 cookie。


我的网站如何运作的示例:

首先我使用 recaptcha 登录,然后获取临时 cookie,

对于我的应用程序中的第一个请求,我想使用这些临时 cookie(已经知道它们)

稍后,每个请求我都需要使用上一个响应中的 cookie(它们随每个请求而变化)

我当前的代码:

def main():
    start_time = time.time()
    keep_running = True
    while keep_running:
        keep_running = execute_data()
        time.sleep(5.0 - ((time.time() - start_time) % 5.0))


def execute_data():
    url = 'https:me.happ.com/rishum/register/confirm'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate',
        'Connection': 'close'
    }

    cookies = dict(rishum='dsfsdf21312zxcasd-' + '39480523')

    try:
        response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

【问题讨论】:

  • 注意:我看到了这个:stackoverflow.com/a/31571805 但问题是我只想第一次使用我自己的cookie,然后根据服务器的响应更新它们
  • 添加了更多可能有帮助的信息

标签: python python-3.x function session python-requests


【解决方案1】:

您几乎已经完成了,但您的字典实现有点偏离。

这就是你要找的:

cookies = {
    "PHPSESSID": "djsgkjhsdjkhj34",
    "authchallenge" : "sdifhshdfiuh34234234",
    "rishum": 'skdhfuihuisdhf-' + '10403111'
    }

try:
    response = requests.get(url, headers=headers, cookies=cookies, allow_redirects=False)

编辑:我现在知道这不是问题,而是您想在会话期间更新 cookie,这是一个简单的示例,说明如何使用 requests.Session 执行此操作

from requests import Session

s = Session()

s.cookies["foo"] = "bar"

r = s.get('https://google.com')

print("Before:")
for cookie in s.cookies:
    print(cookie)

print()

s.cookies["bo"] = "baz"
print("After: ")
for cookie in s.cookies:
    print(cookie)

编辑#2:

为了进一步回答您的问题,这里有一个更好的示例,说明如何在循环中更新 cookie(所有这些,如果需要)。

from requests import Session, cookies

s = Session()

b = s.get('https://google.com')

for cookie in s.cookies:
    print(cookie.value)


# Iterate over cookies
for cookie in s.cookies:

    # You can see we already have this cookie's info in the $cookie variable so lets delete it from the cookie jar
    del s.cookies[cookie.name]
    
    # You can update the values HERE
    # ...
    # Example:
    cookieValue = cookie.value.upper()

    # Then save the new cookie to the cookie jar.
    updated_cookie = cookies.create_cookie(domain=cookie.domain,name=cookie.name,value=cookieValue)
    s.cookies.set_cookie(updated_cookie)

for cookie in s.cookies:
    print(cookie.value)

【讨论】:

  • cookies=cookies 不会一直使用默认cookies吗?
  • 正确,我明白你现在在问什么。我最后的编辑应该是你要找的。​​span>
  • 我想更新它们不仅仅是一个,另外请记住这是循环运行,所以你总是在我不想要之前和之后这样做
  • 我认为我的最新编辑应该回答您的问题。祝你好运!
  • 谢谢,但这仍然不能回答我的问题
猜你喜欢
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
  • 2018-04-04
  • 2018-09-21
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
相关资源
最近更新 更多