1
Septembers 2014 年 12 月 29 日
requests
|
2
winkidney 2014 年 12 月 29 日
使用cookie_jar build_opener
|
3
pc10201 2014 年 12 月 29 日
requests也不带~
|
4
zwzmzd 2014 年 12 月 29 日
试试下面的代码,就是创建一个带cookie处理器的opener
# 用于Cookies维持 cookies = urllib2.HTTPCookieProcessor() opener = urllib2.build_opener(cookies) # 发送一个post请求,注意提交数据的编码encode位置 data = [\ ('methord', 'CheckIsHightLeader'),\ ('leaderNae', LEADER.encode('utf-8'))\ ] request = urllib2.Request(\ url = url,\ data = urllib.urlencode(data)\ ) opener.open(request, timeout = 10).read() |
5
garth OP |
6
mhycy 2014 年 12 月 29 日
那么为了避开这个BUG每次都手工跳转好了...
|
8
R4rvZ6agNVWr56V0 2014 年 12 月 29 日
class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers): return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers) jar = cookielib.MozillaCookieJar('/tmp/sina_cookie_output.txt') opener = urllib2.build_opener(HTTPRedirectHandler, urllib2.HTTPCookieProcessor(jar)) |
9
Septembers 2014 年 12 月 29 日
|
10
garth OP |
11
ahcat 2014 年 12 月 29 日
Requests貌似是带的吧?
http://cn.python-requests.org/zh_CN/latest/user/advanced.html#id2 会话对象让你能够跨请求保持某些参数。它也会在同一个Session实例发出的所有请求之间保持cookies。 s = requests.Session() s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') r = s.get("http://httpbin.org/cookies") print(r.text) # '{"cookies": {"sessioncookie": "123456789"}}' |
12
mengskysama 2014 年 12 月 30 日
我记得requests也有类似问题,在301中set-cookie不会生效,必须手动指定redirects false手动处理这条cookie。
|
13
winkidney 2014 年 12 月 30 日
域不同的话,cookie不能同步的。
|
14
latyas 2015 年 1 月 6 日
昨晚刚看了一下《HTTP权威指南》,大概是对于跨域了并且服务器返回的set-cookies里没有允许新域访问原域的cookies的话,肯定在新域里是不能带原域的cookies的
|