-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawler.java
78 lines (59 loc) · 2.07 KB
/
Crawler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.kevinturner.jv;
import java.io.*;
import java.util.Map.Entry;
import java.util.Map;
import org.jsoup.*;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
public class Crawler {
private final static String loginUrl = "https://home-access.cfisd.net/HomeAccess/Account/LogOn?ReturnUrl=%2fhomeaccess%2f";
private String url;
private Map<String, String> cookies;
public boolean logOn(String username, String password){
try {
Response r = Jsoup.connect(loginUrl)
.data("LogOnDetails.UserName", username)
.data("LogOnDetails.Password", password).data("Database", "20")
.method(Method.POST).timeout(10*1000).ignoreHttpErrors(true).execute();
cookies = r.cookies();
System.out.println(cookies);
int t = 0;
for(Map.Entry<String, String>cookie : cookies.entrySet()){
if(cookie.getValue() != null){
t++;
}
}
System.out.println("t: " + t);
return t == 2;
} catch(IOException e){
System.out.println(e);
}
return false;
}
public Map<String, String> getCookies(){
return cookies;
}
public void setCookies(Map<String, String>cookies){
this.cookies = cookies;
}
public Document crawl() throws IOException{
Connection connection = Jsoup.connect(url).data("ctl00$plnMain$ddlReportCardRuns", "3").timeout(1000*1000).method(Method.POST);
if(this.cookies != null){
for(Entry<String, String>cookie : cookies.entrySet()){
connection.cookie(cookie.getKey(), cookie.getValue());
}
}
return connection.ignoreHttpErrors(false).followRedirects(true).userAgent("Mozilla").referrer("http://www.cfisd.net").get();
}
public Crawler(String url){
this.url = url;
}
public Crawler(){}
public String getUrl(){
return this.url;
}
public void setUrl(String url){
this.url = url;
}
}