您好,登錄后才能下訂單哦!
怎么利用java爬蟲模擬登陸?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、使用工具:Jsoup
jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內(nèi)容。它提供了一套非常省力的API,可通過DOM,CSS以及類似于jQuery的操作方法來取出和操作數(shù)據(jù)。
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、登錄網(wǎng)站,返回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、爬取內(nèi)容
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");// 用戶名,和密碼 } /** * 模擬登陸座位系統(tǒng) * @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();// 獲取響應(yīng) Document d1 = Jsoup.parse(rs.body());// 轉(zhuǎn)換為Dom樹 List<Element> et = d1.select("#form1");// 獲取form表單,可以通過查看頁面源碼代碼得知 // 獲取,cooking和表單屬性,下面map存放post時的數(shù)據(jù) 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);// 設(shè)置用戶名 } if (e.attr("name").equals("tbPassWord")) { e.attr("value", pwd); // 設(shè)置用戶密碼 } if (e.attr("name").length() > 0) {// 排除空值表單屬性 datas.put(e.attr("name"), e.attr("value")); } } /** * 第二次請求,post表單數(shù)據(jù),以及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"); // 設(shè)置cookie和post上面的map數(shù)據(jù) Response login = con2.ignoreContentType(true).method(Method.POST) .data(datas).cookies(rs.cookies()).execute(); // 登陸成功后的cookie信息,可以保存到本地,以后登陸時,只需一次登陸即可 Map<String, String> map = login.cookies(); //下面輸出的是cookie 的內(nèi)容 for (String s : map.keySet()) { System.out.println(s + "=====-----" + map.get(s)); } System.out.println(login.body()); /** * 登錄之后模擬獲取預(yù)約記錄 * * */ 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()); } }
關(guān)于怎么利用java爬蟲模擬登陸問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。