我正在开发一个从特定站点抓取数据的java程序。我使用jSoup作为java模块。
http://data.bitcoinity.org/markets/tradespm/10m?c=e&t=a就是这个网站
我正在尝试在该网站上删除下面的示例数据。
bitflyer 144 14.17%
coinbase 121 11.89%
我试过这样做:
public static void main(String[] args) throws IOException {
try {
Document doc = Jsoup.connect(
"http://data.bitcoinity.org/markets/tradespm/10m?c=e&t=a")
.get();
for (Element table : doc.select("tbody")){
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
System.out.println(row);
System.out.println(tds);
}
}
} catch (IOException e) {
e.printStackTrace();
}
有什么帮助吗?抓取数据
发布于 2018-06-02 03:53:35
你可以通过另一种方式来获取数据。Jsoup
非常适合解析静态内容,但是对于动态内容,您需要找到另一种方法。如果您仔细观察所做的API调用,那么该表就是从http://data.bitcoinity.org/chart_data上的GET请求装载的
您可以在中解析JSON内容以获取数据。我使用OkHttp3来检索数据。
// Imports required
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
public static void main(String[] args) throws IOException {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://data.bitcoinity.org/chart_data")
.build();
Response response = okHttpClient.newCall(request).execute();
JSONObject responseBody = new JSONObject(response.body().string());
JSONArray aggregated = responseBody.getJSONObject("data_additional")
.getJSONArray("aggregated");
for (int i = 0; i < aggregated.length(); ++i) {
JSONObject rowInfo = aggregated.getJSONObject(i);
System.out.println(rowInfo.getString("key") + ", " + rowInfo.getDouble("agg") + ", " + rowInfo.getString("share"));
}
}
上面的代码将生成如下响应
bit-x, 128103.3596, 4.39
bitfinex, 788172.829602228, 27.00
bitflyer, 385999.821606005, 13.22
bitstamp, 345652.20203694, 11.84
coinbase, 320228.256592679, 10.97
gemini, 99966.4119466147, 3.42
hitbtc, 222472.499999999, 7.62
itbit, 161030.4573, 5.52
kraken, 287811.61880974, 9.86
others, 179860.514253696, 6.16
https://stackoverflow.com/questions/50649404
复制相似问题