最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中实现带Cookie的Http的Post请求

Python crifan 13910浏览 0评论

已经实现了如何获得对应的cookie,具体参考:
【已解决】Python中如何获得访问网页所返回的cookie
现在想要把已获得cookie,在http的提交post请求的时候,也同时发送过去。
即,实现带cookie的http的post。

【解决过程】
1.看了这里
http://www.ideawu.net/blog/archives/270.html
的介绍,好像是cookiejar,自动管理的,不需要手动指定之前已有的cookie了。
去写代码试试。

2.后来的结果证实了,接下来,直接调用urllib2.Request,其自动会把cookie送过去的,
因为之前已经是urllib2.build_opener,添加了对应cookiejar去自动管理cookie了。
对应的代码如下:

# first url request
baiduSpaceEntryUrl = "http://hi.baidu.com/motionhouse";
cj = cookielib.CookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
urllib2.install_opener(opener);
resp = urllib2.urlopen(baiduSpaceEntryUrl);

# second time do url request, the cookiejar will auto handle the cookie
loginBaiduUrl = "https://passport.baidu.com/?login";
para = {
    'username'  : username,
    'password'  : password,
    'mem_pass'  : 'on',
    };
postData = urllib.urlencode(para);
req = urllib2.Request(loginBaiduUrl, postData); # urllib2.Request: the HTTP request will be a POST instead of a GET when the data parameter is provided.
req.add_header('User-Agent', gConst['userAgentIE9']);
req.add_header('Content-Type', 'application/x-www-form-urlencoded');
req.add_header('Cache-Control', 'no-cache');
req.add_header('Accept', '*/*');
req.add_header('Connection', 'Keep-Alive');
resp = urllib2.urlopen(req);
respInfo = resp.info();

【总结】

Python中的cookiejar,自动帮我们管理好cookie,用起来,还是蛮方便的。

我们要做的只是,第一次build_opener的时候,把对应的cookiejar传进去,

以后每次的http的request,都会自动包含了cookie,而不需要我们操心了。

转载请注明:在路上 » 【已解决】Python中实现带Cookie的Http的Post请求

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (8)

  1. 可不可以直接在req.add_header里添加cookie,不如这样 req.add_header("Cookie", '''ll="118254"; bid=X2jhrlg47Ko; ps=y;dbcl2="152683308:FxTf9pYhX2E";ck=onzs;ct=y;push_noty_num=0;push_doumail_num=0;''')
    ysour8年前 (2016-10-19)回复
  2. 博主 你滴捐献链接貌似不能用啦~~
    玖兰烨涩10年前 (2014-08-30)回复
  3. 同样的方法用在超星上好像不好使,不知道什么问题。 import cookielib import urllib2 import urllib # first url request baiduSpaceEntryUrl = "http://readsvr.chaoxing.com/reader_877dceb4fe28fc04c4a5a329f8c55843daba794adff46b26ecf9ea0f8aaa66fd3c398ea40bee0504512fcad7f16e2302DCBB2518433F3E268222A641B60752A9.shtml?view=0&url=http://book.chaoxing.com/ebook/read_10184867.html&plan="; cj = cookielib.CookieJar(); opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)); urllib2.install_opener(opener); resp = urllib2.urlopen(baiduSpaceEntryUrl); # second time do url request, the cookiejar will auto handle the cookie loginBaiduUrl = "http://readsvr.chaoxing.com/0682d553ae64f75df9fc01884cbc479b/img15/728F00C2962904EF859D0479F90165E0965A0CEB8997F290FAD8C4CF9D51FC83FBC01F36FC1A23262D718A7F01DEEB3BF229E7BE7C3DF181A768283953744255237B2FDF762B55FC9E1C108BE3E63B21BE3025D5E7BC67179040A030A198A9703D0B21409F794ECDB6975DCAEAC0825A708E/n28/000001?.&uf=ssr&zoom=2"; req = urllib2.Request(loginBaiduUrl); # urllib2.Request: the HTTP request will be a POST instead of a GET when the data parameter is provided. req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1'); req.add_header('Referer', 'http://readsvr.chaoxing.com/reader_877dceb4fe28fc04c4a5a329f8c55843daba794adff46b26ecf9ea0f8aaa66fd3c398ea40bee0504512fcad7f16e2302DCBB2518433F3E268222A641B60752A9.shtml?view=0&url=http://book.chaoxing.com/ebook/read_10184867.html&plan='); req.add_header('Host', 'readsvr.chaoxing.com'); req.add_header('Accept-Language', ' __dxca=4e26bfea-74db-44ed-aac0-fe91507c4213; JSESSIONID=47C22C1EA9DD9714BD4AB75E012A3A0C.jpcxw126; CNZZDATA1889092=cnzz_eid%3D300187298-1391835043-%26ntime%3D1391835043%26cnzz_a%3D0%26ltime%3D1391835044336; 10184867_p=1; 10184867_t=5'); #req.add_header('Content-Type', 'application/x-www-form-urlencoded'); #req.add_header('Cache-Control', 'no-cache'); req.add_header('Accept', 'image/png,image/*;q=0.8,*/*;q=0.5'); req.add_header('Connection', 'Keep-Alive'); resp = urllib2.urlopen(req); respInfo = resp.info(); print respInfo.headers
    Chaoxing10年前 (2014-02-08)回复
  4. Hi,请问first url request下的resp = urllib2.urlopen(baiduSpaceEntryUrl);不是必须的吧? 另外,第二次请求的代码段resp = urllib2.urlopen(req);是不是有误?应该是url而不是req?
    JohnSmith11年前 (2013-04-26)回复
    • 1.应该是必须的。至少是模拟百度登陆过程中,所分析出来的逻辑,需要这一步。 当然,如果你去掉此步骤飞,发现也可以正常模拟登陆。 那么此步骤就是可选的。 2.无误。 详见: urllib2.urlopen(url[, data][, timeout]) Open the URL url, which can be either a string or a Request object. 意思是: urllib2.urlopen的url参数,也可以是Request对象的,即此处的req。 当然,一般来说,我们去使用urllib2.urlopen的时候,都是传递的普通的url地址。
      crifan11年前 (2013-04-26)回复
  5. 这个是不是百度改版前的?这个程序不管用丫
    小媛11年前 (2013-02-17)回复
94 queries in 0.210 seconds, using 22.11MB memory