Python请求在cookie中失败整数

时间:2013-12-02 10:06:56

标签: python python-requests

所以我有一个网站,其中包含一个带整数的cookie,导致请求失败:

import requests
r = requests.get('http://www.c1000.nl/kies-uw-winkel.aspx')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 361, in request
    resp = self.send(prep, **send_kwargs)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/sessions.py", line 464, in send
    r = adapter.send(request, **kwargs)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 369, in send
    r = self.build_response(request, resp)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/adapters.py", line 179, in build_response
    extract_cookies_to_jar(response.cookies, req, resp)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/cookies.py", line 117, in extract_cookies_to_jar
    jar.extract_cookies(res, req)
  File "/usr/lib/python2.7/cookielib.py", line 1645, in extract_cookies
    self.set_cookie(cookie)
  File "/srv/www/li/venv/local/lib/python2.7/site-packages/requests/cookies.py", line 270, in set_cookie
    if cookie.value.startswith('"') and cookie.value.endswith('"'):
AttributeError: 'int' object has no attribute 'startswith'

这是请求中的错误吗? (即它不应该能够处理整数?) 我怎么能避免呢?在获取请求中是否存在避免cookie的事情?

系统信息:

  • Ubuntu 12.04LTS 64bit
  • Python 2.7.3
  • 请求2.0.1

2 个答案:

答案 0 :(得分:1)

这是一个已知的错误,由this GitHub issue跟踪。它将在下一版的请求中修复。

答案 1 :(得分:0)

要处理cookie,您应该使用在请求之间存储cookie的会话:

s = requests.Session()
r = s.get('http://www.c1000.nl/kies-uw-winkel.aspx',timeout=20)

这应该可以解决问题。我不知道为什么你的代码示例会抛出这个异常但只是一个快速的解决方法。