我想获取响应中携带的Cookie(Set-Cookie: name=value; ...
)。
传递信息常量和空列表的引用时:
set_cookies = []
c.getinfo(c.COOKIELIST, set_cookies)
我收到以下错误:
TypeError: c.getinfo() takes exactly 1 argument (2 given)
这很简单。
我也尝试过这个签名:
set_cookies = c.getinfo(c.COOKIELIST)
我收到此错误:
ValueError: invalid argument to getinfo
但是,这有点模糊。
使用 getinfo()
获取 HTTP 状态代码效果很好。
当然,我得到了 200 OK
和 Set-Cookie
header (使用 c.setopt(pycurl.VERBOSE, 1) 进行调试
)。
我当然读过
With very few exceptions, PycURL constants are derived from libcurl constants by removing the CURLINFO_ prefix.
也许这是异常(exception)?
在我看来,该文档相当糟糕,并且不包含任何有关使用 Set-Cookie
发送 cookie 的信息。
引用文献:
最佳答案
调用perform()
后没有方法获取响应cookie。在使用 setopt()
配置时,可以使用传递给 PyCurl 实例的回调函数来捕获服务器发送的 header :
CURLOPT_HEADERFUNCTION
Callback for writing received headers. See CURLOPT_HEADERFUNCTION
示例代码摘录:
set_cookies = []
# closure to capture Set-Cookie
def _write_header(header):
match = re.match("^Set-Cookie: (.*)$", header)
if match:
set_cookies.append(match.group(1))
# use closure to collect cookies sent from the server
c.setopt(pycurl.HEADERFUNCTION, _write_header)
引用文献:
关于python - PyCurl 7.19.0 : get cookies from response using getinfo(pycurl. Cookie 列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650902/