关于爬虫模拟登陆
我是用的是OKHTTP,想模拟登陆学校的教务系统,我看了下网页源码,他提交表单之后先重定向到一个网址,然后那个网址又重定向到另一个网址 ,然后它再get到个人信息页面,我模拟了表单提交的情况,然后他就一直返回登陆页面的源码,这是啥情况?代码贴下:
上面这段是模拟提交的主程序
上面这个是获取隐藏域的值
上面这个是模拟登陆的逻辑
这是登陆页面信息
这是第一次重定向网址
这是第一次重定向传递的参数
这是第一次重定向的消息内容
这是第二次重定向的网址
这是第二次重定向的消息内容
这是最后get到的个人信息的页面
public class HttpDemo {
/**
* 主函数,测试请求,提交表单
*
* @param args
*/
public static void main(String[] args) {
// HttpURLConnectionDemo httpURLConnectionDemo = new HttpURLConnectionDemo();
JsoupDemo demo = new JsoupDemo();
List<String> lists = demo.jsoupDemo();
String username = "150******";
String password = "07****";
String lt = lists.get(0);
String execution = lists.get(1);
String _eventId = lists.get(2);
String rmShow = lists.get(3);
OKhttpDemo oKhttpDemo = new OKhttpDemo();
String cookie = null;
try {
cookie = oKhttpDemo.getCookie();
}catch (Exception e){
e.printStackTrace();
}
oKhttpDemo.sendPostAsync(username,password,lt,execution,_eventId,rmShow,cookie);
}上面这段是模拟提交的主程序
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 通过Jsoup获取表单中隐藏域的值
*/
public class JsoupDemo {
public List<String> jsoupDemo(){
List<String> list = new ArrayList<>();
String url=
"http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal";
try {
//解析URL获得document对象,相当于点击去了URL
Document document = Jsoup.connect(url).timeout(10000).get();
//获取网页源码中id为"casLoginFrom"的标签
Element element = document.getElementById("casLoginForm");
//选择标签内有type属性为hidden的标签
Elements elements = document.getElementsByAttributeValue("type","hidden");
//System.out.println(elements);
//获取标签的value值,并存入list集合
for (Element ele :elements) {
String value = ele.attr("value");
list.add(value);
//System.out.println(value);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
上面这个是获取隐藏域的值
import okhttp3.*;
import java.io.IOException;
public class OKhttpDemo {
/**
* 获取第一次访问的Cookie
* @return
* @throws IOException
*/
public String getCookie() throws IOException{
String url1="http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal";
OkHttpClient c=new OkHttpClient();
Request req=new Request.Builder().url(url1).get().build();
Response response=c.newCall(req).execute();
String Set_Cookie = response.header("Set-Cookie");
String str = Set_Cookie.substring(0, Set_Cookie.indexOf(";"));
System.out.println(str);
return str;
}
/**
* 添加请求头信息,模拟登陆
* @param username
* @param password
* @param lt
* @param execution
* @param _eventId
* @param rmShow
* @param cookie
*/
public void sendPostAsync(String username,String password,String lt,String execution, String _eventId, String rmShow,String cookie){
OkHttpClient client=new OkHttpClient();
//创建一个表单对象
FormBody formBody=new FormBody.Builder()
//添加表单内容
.add("username",username)
.add("password",password)
.add("lt",lt)
.add("execution",execution)
.add("_eventId",_eventId)
.add("rmShow",rmShow)
.build();
//创建一个请求
Request request=new Request.Builder()
//添加url
.url("http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal")
//添加请求头
.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
.addHeader("Accept-Encoding","gzip, deflate")
.addHeader("Accept-Language","zh-CN,zh;q=0.9")
.addHeader("Cache-Control","no-cache")
.addHeader("Connection","keep-alive")
.addHeader("Content-Type","application/x-www-form-urlencoded")
.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36")
.addHeader("Origin"," http://ids.wbu.edu.cn")
.addHeader("Referer","http://ids.wbu.edu.cn/authserver/login?service=http://my.wbu.edu.cn/index.portal")
.addHeader("Cookie",cookie)
//设置请求对象
.post(formBody).build();
Call call=client.newCall(request);
call.enqueue(new Callback(){
//这里是异步操作\
//请求失败时调用
@Override
public void onFailure(Call p1, IOException p2){
// TODO: Implement this method
p2.printStackTrace();
}
@Override
public void onResponse(Call p1, Response p2) throws IOException{
// TODO: Implement this method
//获取响应内容
String responseContent=p2.body().string();
System.out.println(responseContent);
}
});
}
}
上面这个是模拟登陆的逻辑
这是登陆页面信息
这是第一次重定向网址
这是第一次重定向传递的参数
这是第一次重定向的消息内容
这是第二次重定向的网址
这是第二次重定向的消息内容
这是最后get到的个人信息的页面
...全文
相关推荐
CSDN爬虫(三)——网络爬虫模拟登陆两种策略
文章同步:http://blog.csdn.net/wgyscsf
Python3_网络爬虫实践集合。涉及多类型验证码识别、多类型模拟登陆、多
Python3_网络爬虫实践集合。涉及多类型验证码识别、多类型模拟登陆、多类型反反爬措施、APP数据_Python3_WebSpider
java爬虫模拟登陆源码
java爬虫模拟登陆源码:直接导入eclipse,用于爬虫初期网站抓取数据的模拟登陆,即下即用。
Python 爬虫模拟登陆知乎
主要介绍了Python 爬虫模拟登陆知乎的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
Python网络爬虫--模拟登陆教务处网站
本文主要介绍如何利用Python的requests库实现学校教务处网站的模拟登陆。关于教务处网站模拟登陆的典型应用主要有课程格子、超级课程表等,教务处网站形式多种多样,但登陆的基本流程类似,即构建表单-提交表单-实现登陆。本文以我浙的教务处网站为例进行模拟登陆演示。 登陆流程分析 首先打开我浙的教务处网站首页,F12打开开发者工具,输入学号、用户名、验证码,点击登陆之后,通过开发者...


Java SE
