问题描述
大家好,我想在解析一flightradar24.com飞行
我曾尝试与JSOUP和Android,但结果为空。
Hi guys i want parse a flight on flightradar24.comI have tried with JSOUP and Android but results is null.
我看了... JSOUP不支持dinamyc网页。
有一个解决方案?
我希望得到纬度,经度和更多
谢谢你在前进!
I have read... JSOUP doesn't support dinamyc webpages.There is a solution for this?I want get Latitude, longitude, and moreThank you in advance!
推荐答案
在此网站上的航班信息都通过JavaScript Ajax调用查询。因此,页面加载后,他们调用Ajax调用的获得航班信息。如果我们放大它使用一个单独的JavaScript文件的特定部分,比如说在欧洲,他们使用europe_all.js。这实际上将返回一个包含所有航班的JSON详细信息,包括速度的海拔高度等,这都是一个键值对和重点是飞行的ID和价值的细节的数组。
In this site the flight details are polled via JavaScript ajax calls. So after page load they invoke an ajax call to http://db8.flightradar24.com/zones/full_all.js?callback=pd_callback&_=1401126256649 to get the flight details. If we zoom into a particular part it uses a separate JavaScript file, say for Europe they use europe_all.js. This essentially returns a json containing all flights details including the speed the altitude etc. this is maintained as a key-value pair and key being the flight id and value an array of details.
首先,我们需要得到这个JSON,然后分析它来获得飞行的ID这是关键,然后再调用<一个href=\"http://bma.fr24.com/_external/planedata_json.1.4.php?f=36c0ad6&callback=flight_data_service_cb&_=1401126256666\" rel=\"nofollow\">http://bma.fr24.com/_external/planedata_json.1.4.php?f=36c0ad6&callback=flight_data_service_cb&_=1401126256666了解详细内容飞行路径,名称开始时间,结束时间,状态等。创新是作为纬度和经度和前两个元素的数组点的当前位置。
First we need to get this json and then parse it to get flight id which is the key and then again invoke http://bma.fr24.com/_external/planedata_json.1.4.php?f=36c0ad6&callback=flight_data_service_cb&_=1401126256666 to get the details flight trails, the name start time, end time, status etc.. The trails is given as an array of latitude and longitude and the first two elements points the current position.
对于这两个URL中结束一个数字是 System.currentTimeMillis的();
。对于第二个URL参数F,实际上是飞行ID这是第一个JSON的关键。所以,下面的程序将分析这两个JSON和给你的数据。
For both url the ending digit is the System.currentTimeMillis();
. For the second url the argument "f" is actually the flight ID which is the key of first json. So the below program will parse these two json and give you the data.
我用full_all.js这给所有的航班信息这实在是巨大的。限制网络电话我把休息的for循环。所以这个方案只能打印一次飞行的细节。如果除去休息,你会得到的所有航班的所有细节,但中期你,就像一个10000的电话。
I used full_all.js which gives all the flight information which is really huge. To limit the network call i put a break in the for loop. So this program only prints the details of first flight. If you remove the break you'll get all details of all flights but mid you that's like a 10000 calls.
第一个JSON本身让你像下面给出的足够的信息。它只是一个来自第一JSON条目,它说,飞行ID为36c0ae5,注册键0D05AD,当前纬度(25.54),LON(-99.24),速度287,高度16650英尺,等等等等。
The first json itself gives you enough information like the one given below. Its just one entry from first json and it says flight with id "36c0ae5", the registered key "0D05AD", current lat (25.54), lon (-99.24), speed 287, altitude 16650 ft, etc etc
"36c0ae5": [
"0D05AD",
25.54,
-99.24,
287,
16650,
354,
"0610",
"F-KBRO1",
"A320",
"XA-BIC",
1401129559,
"CUN",
"MTY",
"4O321",
0,
-1920,
"AIJ321",
0
]
程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map.Entry;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class FlightDetails {
public static void main(String[] args) throws Exception {
String allFlightsURL = "http://db8.flightradar24.com/zones/full_all.js?callback=pd_callback&_=" + System.currentTimeMillis();
String allFlightsJsonString = getJsonString(allFlightsURL);
JsonParser parser = new JsonParser();
JsonObject allFlightsJsonData = (JsonObject)parser.parse(allFlightsJsonString);
String singleFlightUrl = "http://bma.fr24.com/_external/planedata_json.1.4.php?f=###&callback=flight_data_service_cb&_=";
for(Entry<String, JsonElement> allFlightEntry : allFlightsJsonData.entrySet()){
StringBuilder urlBuilder = new StringBuilder(singleFlightUrl.replaceAll("###", allFlightEntry.getKey())).append(System.currentTimeMillis());
System.out.println(allFlightEntry.getKey() + " = " + allFlightEntry.getValue());
String singleFlightJsonString = getJsonString(urlBuilder.toString());
JsonObject singleFlightJsonData = (JsonObject)parser.parse(singleFlightJsonString);
for(Entry<String, JsonElement> singleFlightEntry : singleFlightJsonData.entrySet()){
System.out.println(singleFlightEntry.getKey() + " = " + singleFlightEntry.getValue());
}
break; // Breaking to avoid huge network calls.
}
System.out.println("Done");
}
private static String getJsonString(String allFlightsURL) throws IOException {
HttpURLConnection connection = (HttpURLConnection) ((new URL(allFlightsURL).openConnection()));
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("GET");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder buffer = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.substring(buffer.indexOf("(") + 1, buffer.lastIndexOf(")"));
}
}
这篇关于解析FlightRadar24和JSOUP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!