我已經寫了一個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);
}
,我想也收到錯誤郵件,如果再殺了問題。任何幫助請
「我寫過」。你確定嗎? –