gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:34:12 25 4
gpt4 key购买 nike

我想从这个网址抓取数据:http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1 .我想提取 (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);
}
}

运行后我遇到了这个异常:

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)

最佳答案

首先连接到默认登录页面 ( http://www.airfrance.fr/vols/paris+tunis )。

从响应中,我们可以使用 response.cookies() 获取所需的 cookie,并使用 将其设置为连接到查询页面 (http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1)。 cookies (响应.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, ...

关于java - 位置 0 处的意外字符 (B),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com