怎么利用java爬虫模拟登陆?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
一、使用工具:Jsoup
jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。
1、确定想要爬取的url
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map.Entry;
import java.util.Set;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class SplitTable {
public static void main(String[] args) throws IOException {
//想要爬取的url
String url = "http://jwcnew.nefu.edu.cn/dblydx_jsxsd/xskb/xskb_list.do?
Ves632DSdyV=NEW_XSD_PYGL";
String username = "";
String password = "";
String sessionId = getSessionInfo(username,password);
spiderWebSite(sessionId,url);
}
2、获取sessionId
private static String getSessionInfo(String username,String password)
throws IOException{
3、登录网站,返回sessionId信息
Connection.Response res = Jsoup.connect(http://jwcnew.nefu.edu.cn/dblydx_jsxsd/xk/LoginToXk)
4、获得sessionId
String sessionId = res.cookie("JSESSIONID");
System.out.println(sessionId);
return sessionId;
}
5、爬取内容
private static void spiderWebSite(String sessionId,String url) throws IOException{
//爬取
Document doc = Jsoup.connect(url).cookie("JSESSIONID", sessionId).timeout(10000).get();
Element table = doc.getElementById("kbtable");
//System.out.println(table);
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream("F:/table.html")));
bw.write(new String(table.toString().getBytes()));
bw.flush();
bw.close();
}
}
实例代码扩展:
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class LoginDemo {
public static void main(String[] args) throws Exception {
LoginDemo loginDemo = new LoginDemo();
loginDemo.login("16xxx20xxx", "16xxx20xxx");// 用户名,和密码
}
/**
* 模拟登陆座位系统
* @param userName
* 用户名
* @param pwd
* 密码
*
* **/
public void login(String userName, String pwd) throws Exception {
// 第一次请求
Connection con = Jsoup
.connect("http://lib???.?????????.aspx");// 获取连接
con.header("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");// 配置模拟浏览器
Response rs = con.execute();// 获取响应
Document d1 = Jsoup.parse(rs.body());// 转换为Dom树
List<Element> et = d1.select("#form1");// 获取form表单,可以通过查看页面源码代码得知
// 获取,cooking和表单属性,下面map存放post时的数据
Map<String, String> datas = new HashMap<>();
for (Element e : et.get(0).getAllElements()) {
//System.out.println(e.attr("name")+"----Little\n");
if (e.attr("name").equals("tbUserName")) {
e.attr("value", userName);// 设置用户名
}
if (e.attr("name").equals("tbPassWord")) {
e.attr("value", pwd); // 设置用户密码
}
if (e.attr("name").length() > 0) {// 排除空值表单属性
datas.put(e.attr("name"), e.attr("value"));
}
}
/**
* 第二次请求,post表单数据,以及cookie信息
*
* **/
Connection con2 = Jsoup
.connect("http://lib???.?????????.aspx");
con2.header("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
// 设置cookie和post上面的map数据
Response login = con2.ignoreContentType(true).method(Method.POST)
.data(datas).cookies(rs.cookies()).execute();
// 登陆成功后的cookie信息,可以保存到本地,以后登陆时,只需一次登陆即可
Map<String, String> map = login.cookies();
//下面输出的是cookie 的内容
for (String s : map.keySet()) {
System.out.println(s + "=====-----" + map.get(s));
}
System.out.println(login.body());
/**
* 登录之后模拟获取预约记录
*
* */
Connection con_record = Jsoup
.connect("http://lib???.?????????.aspx");// 获取连接
con_record.header("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");// 配置模拟浏览器
con_record.cookies(datas);
Response record = con_record.ignoreContentType(true)
.method(Method.GET)
.cookies(rs.cookies())
.execute();
System.out.println(record.body());
}
}
关于怎么利用java爬虫模拟登陆问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。