位置0的意外字符(B) [英] Unexpected character (B) at position 0

查看:237
本文介绍了位置0的意外字符(B)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从以下网址中抓取数据:

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)

推荐答案

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

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

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

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

注意:此处可能不需要设置用户代理和引荐来源网址,但这也没有害处并且可以稳定抓取.

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, ...

这篇关于位置0的意外字符(B)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆