python requests模块post获取302页面的cookies值,然后get重定向到另一个页面带上在302页上set-cookies后的值,怎么操作。

elliott_c 2016-03-24 02:43:27
python requests模块post获取302页面的cookies值,然后get重定向到另一个页面带上在302页上set-cookies后的值,怎么操作。

我在抓取我们学校的服务器时遇到如下情况:



firefox浏览器请求情况,首先我输入自己的学号密码,因为是在浏览器上操作的所以肯定post提交之前先带上了get页面时自己的header和cookie ,所以post时的请求头信息如下:



那么我想,在用python request请求时,先得get获取cookie和header然后带着请求和cookie模拟浏览器再post。但我发现此处post后是302(重定向)而且在302页面上已经set-cookie了,即验证后的cookie值!


然后服务器自动重定向到另一个页面,即详细内容页面(get方式且带着在302页面上获取的已验证的cookie值)如下图:


那么我该如何用urllib2模块或者 request模块写呢?

我的思路 先get获得请求的header和cookie,post给302,让302返回header和cookie,然后再带着302返回的headers和cookies去get请求详细页面。但我失败了。。。。。。求大神指教= =。。。。。。。。。。。。。。。。
要用学号模拟,请加我qq1352133162
...全文
1711 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
elliott_c 2016-04-01
  • 打赏
  • 举报
回复
我用了selenium+python 很轻松的解决了这个问题,但是模拟浏览器后再进行爬去的操作,成本高,效率低,容易卡在页面出错==
「已注销」 2016-03-25
  • 打赏
  • 举报
回复
可能网站用了cas认证,所以会重定向,登录之后先进行cas认证 认证通过了才能跳转到登录后的学生管理系统界面
Java的世界里,HttpClient 是一个功能强大的Http请求库,然而接口非常复杂,设计上遵从正交性,简单的请求也需要写比较多的代码,更不要说隐藏在各种细节里面的高级用法了。Requests,  是一个模仿python requests 模块来设计的Http lib,拥有简单而灵活的API,在容易使用的同时,又能够满足各种高级定制的使用,可是说是当前最好用的Java Http Client Lib。 简单的请求示例:String url = ...; Response resp = Requests.get(url).text(); // post 和其他方法 resp = Requests.post(url).text(); resp = Requests.head(url).text(); //读取Http Response  int statusCode = resp.getStatusCode(); Headers headers = resp.getHeaders(); Cookies cookies = resp.getCookies(); String body = resp.getBody(); //response 返回其他类型 resp = Requests.get(url).text("UTF-8"); // get response as bytes Response resp1 = Requests.get(url).bytes(); // save response as file  Response resp2 = Requests.get(url).file("/path/to/save/file"); // url 参数: Map map = new HashMap(); map.put("k1", "v1"); map.put("k2", "v2"); Response resp = Requests.get(url).param("key1", "value1").params(map)         //.params(new Parameter(...), new Parameter(...))         .text(); // 请求头 Response resp = Requests.get(url).header("key1", "value1").headers(map)         //.headers(new Header(...), new Header(...))         .text(); // 添加Cookie: Map cookies = new HashMap(); Response resp = Requests.get(url).cookie("key1", "value1").cookies(map)         //.cookies(new Cookie(...), new Cookie(...))         .text(); //  设置 userAgent Response resp = Requests.get(url).userAgent(userAgent).text(); // 增加请求数据(post, put, patch方法) // send form-encoded data. x-www-form-urlencoded header will be send automatically Response resp = Requests.post(url).data(map).text(); // send string data String str = ...; resp = Requests.post(url).data(str, "UTF-8").text(); // send from inputStream InputStream in = ... resp = Requests.post(url).data(in).text(); // multipart 请求, 用于文件上传: Response resp = Requests.post(url).data(map).multiPart("ufile", "/path/to/file")         .multiPart(..., ...).text();请求设置://禁止自动重定向

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧