Guest User

Untitled

a guest
Oct 31st, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import org.jsoup.Connection;
  2. import org.jsoup.Jsoup;
  3. import org.jsoup.nodes.Document;
  4. import org.jsoup.select.Elements;
  5.  
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8.  
  9.  
  10. public class Parser {
  11.     private ArrayList<Lesson> lesson = new ArrayList<>();
  12.  
  13.     public void start() throws IOException {
  14.         // 1. ПОЛУЧАЕМ запрос на форму входа, скрытые поля и куки
  15.         Connection.Response loginForm = Jsoup.connect("https://nehai.by/ej/templates/login_parent.php")
  16.                 .method(Connection.Method.GET)
  17.                 .execute();
  18.         // в итоге получаем скрытые поля, динамически генерируемые сервером
  19.         Document doc = loginForm.parse();
  20.         String S_Code = doc.getElementsByAttributeValue("id","S_Code").attr("value");
  21.  
  22.  
  23.         //2. POST-аутентификация
  24.         Connection.Response postAuth = Jsoup.connect("https://nehai.by/ej/ajax.php")
  25.                 .userAgent("Firefox ...")
  26.                 .referrer("https://nehai.by/ej/templates/login_parent.php")
  27.                 .data("action", "login_parent")
  28.                 .data("student_name", "-")
  29.                 .data("group_id", "-")
  30.                 .data("birth_day", "-")
  31.                 .data("S_Code", S_Code)
  32.                 .cookies(loginForm.cookies()) // важно!
  33.                 .method(Connection.Method.POST)
  34.                 .execute();
  35.  
  36.         //3. Вход в кабинет
  37.         Connection.Response auth = Jsoup.connect("https://nehai.by/ej/templates/parent_journal.php")
  38.                 .cookies(loginForm.cookies()) // важно!
  39.                 .method(Connection.Method.GET)
  40.                 .execute();
  41.  
  42.         Document document = auth.parse();
  43.         Elements subjects = document.select("div.leftColumn").select("tr");
  44.         Elements marks = document.select("div.rightColumn").select("tr");
  45.  
  46.  
  47.         for(int i = 2; i<subjects.size()-1; i++)
  48.             lesson.add(new Lesson(subjects.eq(i).text(), marks.eq(i).select("span.mar").text()));
  49.     }
  50.  
  51.     public ArrayList<Lesson> getLesson() {
  52.         return lesson;
  53.     }
  54. }
Add Comment
Please, Sign In to add comment