Unexpected character (B) at position 0

纵然是瞬间 提交于 2020-01-04 05:12:36

问题


I want to scrape data from this url: http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1. I want to extract ( Date + Price + Price HT+ Taxe) and then save them into an Excel file . I used this code:

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

After running I faced this exception :

Unexpected character (B) at position 0.
    at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
    at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
    at MoisAirfrancee.main(MoisAirfrancee.java:47)

回答1:


First connect to the default landing page (http://www.airfrance.fr/vols/paris+tunis).

From the response we can grab the needed cookie(s) with response.cookies() and set it/them for the connection to the query page (http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1) with .cookies(response.cookies())

Note: setting user agent and referrer might not be needed here, but it also doesn't harm and might stabilize the scraping.

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

Output:

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


来源:https://stackoverflow.com/questions/38965937/unexpected-character-b-at-position-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!