Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scraper;
- import java.io.IOException;
- import com.gargoylesoftware.htmlunit.BrowserVersion;
- import com.gargoylesoftware.htmlunit.WebClient;
- import com.gargoylesoftware.htmlunit.html.HtmlForm;
- import com.gargoylesoftware.htmlunit.html.HtmlPage;
- import com.gargoylesoftware.htmlunit.html.HtmlSelect;
- import model.Lesson;
- import model.Week;
- public class Scraper {
- private WebClient client;
- public Scraper() {
- this.client = new WebClient();
- }
- private String fetchContent(int gid, boolean next) throws IOException {
- HtmlPage page;
- HtmlForm form;
- HtmlSelect select;
- String content;
- // Open the website the normal way
- this.client.getPage("http://www.formadep.fr/infolarochelle");
- // Then go to the inner webpage
- page = this.client.getPage("http://www.formadep.fr/extretud/exte_edtgroupe.aspx");
- // Form selection
- form = page.getFormByName("_ctl0");
- // Select a group
- select = form.getSelectByName("dd1");
- select.setSelectedAttribute(select.getOptionByValue("" + gid), true);
- // Check the radio button if we want the next week
- if (next) {
- form.getRadioButtonsByName("semaine").get(1).setChecked(true);
- }
- // Submit
- page = form.getInputByName("b1").click();
- // Get the text and close
- content = page.asText();
- this.client.closeAllWindows();
- return content;
- }
- private void findWeek(int gid, boolean next, Week week) {
- try {
- String content;
- int day=0;
- content = fetchContent(gid, next);
- for (String i : content.split("\n")) {
- if (i.contains("Matin") || i.contains("Après-midi")) {
- day = 0;
- }
- else if (i.equals("\t")) {
- day++;
- }
- else if (i.equals("\t\t")) {
- day+=2;
- }
- else if (i.contains(":")) {
- String[] s = i.split("\t");
- week.getDay(day).addLesson(new Lesson(s[0], s[1], s[2]));
- }
- }
- } catch (IOException e) {
- System.out.println("IO exception");
- }
- }
- public void find(int gid, Week week, Week nextWeek) {
- findWeek(gid, false, week);
- findWeek(gid, true, nextWeek);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement