Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 位置0处的意外字符(B)_Java_Json_Web Scraping_Jsoup_Jxl - Fatal编程技术网

Java 位置0处的意外字符(B)

Java 位置0处的意外字符(B),java,json,web-scraping,jsoup,jxl,Java,Json,Web Scraping,Jsoup,Jxl,我想从以下url中提取数据:。 我想提取(Date+Price+Price-HT+Taxe),然后将它们保存到Excel文件中。我使用了以下代码: import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import org.j

我想从以下url中提取数据:。 我想提取(
Date+Price+Price-HT+Taxe
),然后将它们保存到Excel文件中。我使用了以下代码:

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.jsoup.Jsoup;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.javascript.host.dom.Document;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class MoisAirfrancee {

    public static void main(String[] args)throws FailingHttpStatusCodeException, MalformedURLException, IOException, RowsExceededException, WriteException{

        Map<String, Integer> prices = new TreeMap<String, Integer>(); 
        File f=new File("C:\\Users\\tahab_000\\Desktop\\Test.xls");
        WritableWorkbook myexcel=Workbook.createWorkbook(f);
        WritableSheet mysheet=myexcel.createSheet("mySheet", 0);        

        try {
            org.jsoup.nodes.Document doc = Jsoup.connect("http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1").get();

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

            obj = (JSONObject) obj.get("days");

            for (Iterator<?> iterator = obj.keySet().iterator(); iterator.hasNext();) {
                String key = (String) iterator.next();
                JSONObject dateObject = (JSONObject) obj.get(key);
                Double price = (Double) dateObject.get("price");
                int roundedPrice = (int) Math.ceil(price); 

                prices.put(key, roundedPrice);          

            }
            int j=1;

            for (String key : prices.keySet()) {

                addLabel(mysheet, 0, 0, "Date" );
                addLabel(mysheet, 1, 0, "Prix" );
                addLabel(mysheet, 1, j, prices.get(key).toString()+"€" );
                addLabel(mysheet, 0, j, key );

                j++;

                System.out.println(key + ": " + prices.get(key) + " €");
            }
        }catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        myexcel.write();

        myexcel.close();

    }
    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);
          }
}

首先连接到默认登录页()

从响应中,我们可以使用
response.cookies()
获取所需的cookie,并使用
.cookies(response.cookies())
将其设置为与查询页面()的连接

注意:这里可能不需要设置用户代理和推荐人,但这也不会造成伤害,并且可能会稳定刮削

输出:

{"idMonth":10,"month":"Novembre","bestPrice":270.0,"isLowest":false,"isAvailable":true, ...

如果您确定URL返回JSON,那么您似乎正在输入要解析为JSON的数据。当我尝试这个URL时,我得到了一个正常的网页,在Jsoup中使用该代码时,我遇到了一个异常。如果你能帮我,我怎么能把我的代码发给你?在这段代码中或在其他点将其添加到代码库时,请提前感谢?您可以发布您的电子邮件地址/网页/github/。。。然后我可以联系你,塔哈。benmohamed@esprit.tn我向您致意,您很快就会收到一封邮件。
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())
                .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")
                .timeout(2000)
                .get();

String jsonResponse = doc.text();

System.out.println(jsonResponse);
{"idMonth":10,"month":"Novembre","bestPrice":270.0,"isLowest":false,"isAvailable":true, ...