2016-09-20 7 views
0

私はスクレイピングのJavaプログラムを作成しました。私は特定の時間に毎日スクレイピングを実行するように自動化したいと思います。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 jobsたとえば、これらは毎日(または毎時、毎月)所定の時刻に実行できます。しかし、失敗したCronジョブのエラー報告に関しては、* nix空間でもっと進んだノウハウが必要になるかもしれません。おそらくあなたはこの時点で取り組むことに興味がありません。

Java自体には、定期的にジョブを実行できる多数のスケジューラーフレームワークがあります。 Quartz Schedulerは1です。私がまだそれを試していないうちに、Obsidian Schedulerは私の同僚のいくつかが最近、Quartzの代わりに私にもたらした別のものです。しかし、これらの名前はすべて単なる逸話であり、その存続可能性は時間とともに変化する可能性があります。これらのことはすでにサードパーティライブラリとして使用するために作成されており、どのライブラリが最良のものかを調べる必要があります。

+0

自分のコードをjarファイルにデプロイする必要がありますか? –

0

crontabで実行してください。

エラーメールについて...これは全く異なる質問であり、あなた次第です。

+0

私は窓を使用しています –

+0

@hamzaelhadjその後、Windows用の "crontab"または何か – djechlin

+0

ありがとう@djechlin –

関連する問題