解析网站 booking.uz.gov.ua
Posted
技术标签:
【中文标题】解析网站 booking.uz.gov.ua【英文标题】:Parsing of the site booking.uz.gov.ua 【发布时间】:2015-10-05 17:13:47 【问题描述】:此代码从网站 booking.uz.gov.ua 解析。但不知为何,他不想工作。谁能说明为什么不起作用或修复它? 谁能给点建议?
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 ua.gov.uz.booking.uz.main(uz.java:137)
package ua.gov.uz.booking;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class uz
static String html = "";
static String cookie = "";
static String token = "";
static String error = "";
static Map<String, List<String>> headers = null;
static void fetchHtml()
try
URL url = new URL("http://booking.uz.gov.ua/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
headers = conn.getHeaderFields();
String line;
while ((line = rd.readLine()) != null)
html += line;
rd.close();
catch (Exception e)
error = e.getMessage();
static void parseCookie()
List<String> cookies = headers.get("Set-Cookie");
for (String current_cookie : cookies)
if (current_cookie.startsWith("_gv_sessid"))
cookie = current_cookie;
break;
static void parseToken()
String adapter = "var token;localStorage=setItem:function(key, value)if(key==='gv-token')token=value;";
Pattern pattern = Pattern.compile("\\$\\$_=.*~\\[\\];.*\"\"\\)\\(\\)\\)\\(\\);");
Matcher matcher = pattern.matcher(html);
if (matcher.find())
String obfuscated = matcher.group(0);
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("javascript");
try
engine.eval(adapter + obfuscated);
catch (ScriptException e)
error = e.getMessage();
token = engine.get("token").toString();
static String getStationId(String name)
String json = "";
try
URL url = new URL("http://booking.uz.gov.ua/en/purchase/station/" + name + "/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
json += line;
rd.close();
catch (Exception e)
error = e.getMessage();
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.put("json", json);
try
engine.eval("var station_id = JSON.parse(json).value[0].station_id");
catch (ScriptException e)
error = e.getMessage();
return engine.get("station_id").toString();
static String getData(String fromName, String toName, String date)
fetchHtml();
parseCookie();
parseToken();
String from = getStationId(fromName);
String to = getStationId(toName);
String json = "";
try
URL url = new URL("http://booking.uz.gov.ua/en/purchase/search/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.setRequestProperty("GV-Token", token);
conn.setRequestProperty("GV-Ajax", "1");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Referer", "http://booking.uz.gov.ua/en/");
conn.setRequestMethod("POST");
String urlParameters = MessageFormat.format("station_id_from=0&station_id_till=1&date_dep=2" +
"&time_dep=00:00&time_dep_till=24:00", from, to, date);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
json += line;
rd.close();
catch (Exception e)
error = e.getMessage();
return json;
static String getData(String fromName, String toName)
return getData(fromName, toName, new SimpleDateFormat("MM.dd.yyyy").format(new Date()));
public static void main(String[] args)
if (args.length < 2)
System.out.println("Онлайн резервування та придбання квиткiв - Укрзалізниця");
System.out.println("Введите: <start_station> <end_station> [MM.DD.YYYY]");
System.exit(1);
String data;
if (args.length > 2)
data = getData(args[0], args[1], args[2]);
else
data = getData(args[0], args[1]);
System.out.println(data);
【问题讨论】:
【参考方案1】:第 137 行是 args[0] = "dsa"。如果 0 是 IndexOutOfBound,这意味着 args 是一个大小为 0(即空)的数组。因此,您不能访问它的索引 0,因为这需要一个大小 >= 1 的数组。
如何启动程序会影响 args 数组的内容(包含通过控制台启动时传递给程序的参数)。您是否检查过使用断点启动时的 args 是什么?你如何启动你的程序? (在 ide 中,还是通过控制台?)
【讨论】:
固定 main()。但在控制台中显示我:Онлайн резервування та придбання квиткiв - Укрзалізниця Введите:我猜你是直接从 main 方法运行程序然后初始化args
。如果您从其他程序调用 main 方法,则不必初始化 args
public static void main(String[] args)
args = new String[5];
args[0] = "dsa";
.......
【讨论】:
固定 main()。但在控制台中显示我:Онлайн резервування та придбання квиткiв - Укрзалізниця Введите:以上是关于解析网站 booking.uz.gov.ua的主要内容,如果未能解决你的问题,请参考以下文章