使用Python请求在请求之间设置新的Cookie [英] Set new cookie between requests with Python Requests

查看:2815
本文介绍了使用Python请求在请求之间设置新的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在这样做,但它在最后一行与 TypeError:expected string or buffer 失败。

I'm doing this right now, but it fails at that last line with TypeError: expected string or buffer.

import requests
from urllib.parse import urlparse

url = 'some url'

s = requests.Session()
s.headers.update({
    'Origin':urlparse(url).netloc,
    'Referer':url
})


r = s.get(url)

s.cookies['cookie1'] = 25
s.cookies['cookie2'] = 25

r = s.post(
    url,
    {'param':'value1', 'param2':'value2'},
    headers={'X-Requested-With':'XMLHttpRequest'}
)

code> Session ?我对Python很新,所以我可能会困惑的东西。使用Python 3.4.1。

What's the correct way to update the cookies when using Session? I'm pretty new to Python, so I might have confused something. Using Python 3.4.1.

跟踪回溯:

Traceback (most recent call last):
  File "file.py", line 37, in <module>
    {'param':'value1', 'param2':'value2'}
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 498, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 422, in request
    prep = self.prepare_request(req)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 360, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 296, in prepare
    self.prepare_cookies(cookies)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/models.py", line 491, in prepare_cookies
    cookie_header = get_cookie_header(self._cookies, self)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/cookies.py", line 134, in get_cookie_header
    jar.add_cookie_header(r)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/cookiejar.py", line 1329, in add_cookie_header
    attrs = self._cookie_attrs(cookies)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/cookiejar.py", line 1288, in _cookie_attrs
    self.non_word_re.search(cookie.value) and version > 0):
TypeError: expected string or buffer


推荐答案

p> Cookie值是字符串,而不是整数。将其设置为:

Cookie values are strings, not integers. Set them as such:

s.cookies['cookie1'] = '25'
s.cookies['cookie2'] = '25'

演示:

>>> import requests
>>> from urllib.parse import urlparse
>>> url = 'http://httpbin.org/cookies'
>>> s = requests.Session()
>>> s.headers.update({
...     'Origin':urlparse(url).netloc,
...     'Referer':url
... })
>>> r = s.get(url)
>>> s.cookies['cookie1'] = '25'
>>> s.cookies['cookie2'] = '25'
>>> r = s.get(url, headers={'X-Requested-With':'XMLHttpRequest'})
>>> print(r.text)
{"cookies": {"cookie1": "25", "cookie2": "25"}}

这篇关于使用Python请求在请求之间设置新的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆