我正在开发一个项目,该项目将解析数据库(特别是纽约时报在线数据库http://query.nytimes.com/search/sitesearch/#/McCain/from20061201to20080603/allresults/1/allauthors/oldest/)的html代码来查找某些关键字,但是我从oracle获得的代码无法获取完整的html。我之前已经能够在其他项目中使用此代码,所以我不确定为什么它没有读取所有 html。
谢谢:)
URL oracle = new URL("http://query.nytimes.com/search/sitesearch/#/McCain/from20061201to20080603/allresults/1/allauthors/oldest/");
URLConnection yc = oracle.openConnection();
yc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
System.out.println(yc.getContentType());
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
最佳答案
纽约时报搜索页面使用页面 JavaScript 来调用 API,该 API 以 JSON 形式返回搜索结果,然后将其格式化为 HTML。由于您希望收集数据,因此使用此 API 可能就是您想要的,并且具有不需要抓取并以结构化、易于处理的格式返回数据的优点。您必须使用浏览器的开发工具来弄清楚 API 是如何工作的,或者查看其他人是否已经记录了它。这是“foo”的简单查询的样子 -
http://query.nytimes.com/svc/add/v1/sitesearch.json?q=foo&spotlight=true&facet=true
关于java - 用 Java 获取完整的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404362/