2016-09-20 50 views
0

我已經寫了一個java程序的刮,我想自動化它在特定的時間每天運行scraping。如何自動化一個java程序

這是代碼

private static class DateObject{ 

     private Double taxes; 
     private Double price; 
     private Double htPrice; 

     public DateObject(Double price, Double htPrice, Double taxes){ 
      this.taxes = taxes; 
      this.price = price; 
      this.htPrice = htPrice; 
     } 

     public Double getTaxes() { 
      return taxes; 
     } 

     public Double getPrice() { 
      return price; 
     } 

     public Double getHtPrice() { 
      return htPrice; 
     } 
    } 

    public static void main(String[] args) { 

     Map<String, DateObject> prices = new TreeMap<String, DateObject>(); 
     File f = new File(System.getProperty("user.home") + "\\Desktop\\Test.xls"); 

     WritableWorkbook myexcel = null; 

     try { 

      myexcel = Workbook.createWorkbook(f); 
      WritableSheet mysheet = myexcel.createSheet("AirFrance ", 0); 

      Response response = Jsoup 
        .connect("http://www.airfrance.fr/vols/paris+tunis") 
        .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36") 
        .method(Method.GET) 
        .timeout(2000) 
        .execute(); 

      Document doc = Jsoup 
        .connect("http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1") 
        .cookies(response.cookies()) 
        .timeout(2000) 
        .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36") 
        .referrer("http://www.airfrance.fr/vols/paris+tunis").get(); 

      JSONObject obj = (JSONObject) new JSONParser().parse(doc.text()); 

      JSONArray dates = (JSONArray) obj.get("days"); 

      JSONObject dateObject; 

      for(Object o : dates){ 
       if (o instanceof JSONObject) { 
        dateObject = ((JSONObject)o); 
        prices.put(dateObject.get("dallasDate").toString(), new DateObject((Double)dateObject.get("price"), (Double)dateObject.get("HTprice"), (Double)dateObject.get("taxes"))); 
       } 
      } 

      addLabel(mysheet, 0, 0, "Date"); 
      addLabel(mysheet, 1, 0, "Prix [€]"); 
      addLabel(mysheet, 2, 0, "PrixHt [€]"); 
      addLabel(mysheet, 3, 0, "Taxes [€]"); 

      int rowIndex = 1; 
      DateObject date; 

      for (String key : prices.keySet()) { 
       date = prices.get(key); 
       addLabel(mysheet, 0, rowIndex, key); 
       addLabel(mysheet, 1, rowIndex, ""+date.getPrice()); 
       addLabel(mysheet, 2, rowIndex, ""+date.getHtPrice()); 
       addLabel(mysheet, 3, rowIndex, ""+date.getTaxes()); 
       rowIndex++; 
      } 

     myexcel.write(); 

     System.out.println("Scraping finished without errors."); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (WriteException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       myexcel.close(); 
      } catch (WriteException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    private static void addLabel(WritableSheet sheet, int column, int row, String s) 
      throws WriteException, RowsExceededException { 
     Label label; 
     label = new Label(column, row, s); 
     sheet.addCell(label); 
    } 

,我想也收到錯誤郵件,如果再殺了問題。任何幫助請

+0

「我寫過」。你確定嗎? –

回答

0

有很多方法可以做到這一點。例如:這些可以在每天(或每小時或每月)的給定時間運行。但是,當談到失敗的Cron作業的錯誤報告時,需要在* nix空間中使用一些更先進的技術,這可能是您目前沒有興趣處理的。

Java本身有許多調度程序框架,可以定期執行您的工作。 Quartz Scheduler就是其中之一。雖然我還沒有嘗試過,Obsidian Scheduler是我的一些同事最近爲我提供的Quartz替代品。但這些名字都只是軼事,他們的生存能力隨時間而變化。只要說明一下,這些東西已經被創建供您作爲第三方庫使用,您應該研究它們以確定哪一個最適合您。

+0

我應該將我的代碼部署到jar文件嗎? –

0

crontab下運行它。

至於錯誤郵件...這是一個完全不同的問題,由您決定。

+0

我正在使用windows –

+0

@hamzaelhadj然後谷歌「crontab for windows」或什麼 – djechlin

+0

謝謝@djechlin –