Advertisement
GeeckoDev

Scraper

Dec 1st, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package scraper;
  2.  
  3. import java.io.IOException;
  4.  
  5. import com.gargoylesoftware.htmlunit.BrowserVersion;
  6. import com.gargoylesoftware.htmlunit.WebClient;
  7. import com.gargoylesoftware.htmlunit.html.HtmlForm;
  8. import com.gargoylesoftware.htmlunit.html.HtmlPage;
  9. import com.gargoylesoftware.htmlunit.html.HtmlSelect;
  10.  
  11. import model.Lesson;
  12. import model.Week;
  13.  
  14. public class Scraper {
  15.     private WebClient client;
  16.    
  17.     public Scraper() {
  18.         this.client = new WebClient(BrowserVersion.FIREFOX_3_6);
  19.     }
  20.    
  21.     private String fetchContent(int gid) throws IOException {
  22.         HtmlPage page;
  23.         HtmlForm form;
  24.         HtmlSelect select;
  25.        
  26.         this.client.getPage("http://www.formadep.fr/infolarochelle");
  27.         page = this.client.getPage("http://www.formadep.fr/extretud/exte_edtgroupe.aspx");
  28.    
  29.         form = page.getFormByName("_ctl0");
  30.         select = form.getSelectByName("dd1");
  31.         select.setSelectedAttribute(select.getOptionByValue("" + gid), true);
  32.         page = form.getInputByName("b1").click();
  33.            
  34.         return page.asText();
  35.     }
  36.    
  37.     public void find(int gid, Week week, Week nextWeek) {
  38.         try {
  39.             String content;
  40.             int jour=0;
  41.            
  42.             content = fetchContent(gid);
  43.             System.out.println(content);
  44.            
  45.             for (String i : content.split("\n")) {
  46.                 if (i.contains("Matin") || i.contains("Après-midi")) {
  47.                     jour = 0;
  48.                 }
  49.                 else if (i.equals("\t")) {
  50.                     jour++;
  51.                 }
  52.                 else if (i.equals("\t\t")) {
  53.                     jour+=2;
  54.                 }
  55.                 else if (i.contains(":")) {
  56.                     String[] s = i.split("\t");
  57.                    
  58.                     week.getDay(jour).addLesson(new Lesson(s[0], s[1], s[2]));
  59.                 }
  60.             }
  61.         } catch (IOException e) {
  62.             System.out.println("IO exception");
  63.         }
  64.        
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement