https://okky.kr/article/390581
@RequestMapping(value="/search/okky", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> searchOkky(@RequestParam(value="keyword") String keyword,
@RequestParam(value="maxpage", defaultValue="5") int maxpage,
HttpServletRequest request) throws Exception{
keyword = URLEncoder.encode(keyword, "UTF-8");
int count = 1;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet;
String URL = "http://okky.kr/articles/questions?query="
+keyword+"&sort=id&order=desc";
List<Article> list = new ArrayList<Article>();
while(true){
httpGet = new HttpGet(URL);
Document doc;
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
doc = Jsoup.parse(httpClient.execute(httpGet, responseHandler));
Elements nodes = doc.select(".list-group-item h5");
nodes.parallelStream().forEach(e -> {
Article article = new Article();
article.setTitle(e.text());
article.setHref("http://okky.kr" + e.getElementsByTag("a").attr("href"));
list.add(article);
});
Elements pages = doc.select(".pagination li");
Element next = pages.last();
boolean isNext = false;
if(next != null)
isNext = next.classNames().contains("next");
if(!isNext || count >= maxpage)
break;
URL = "http://okky.kr" + next.getElementsByTag("a").attr("href");
count++;
}
logger.info("Okky Search Request from "+request.getRemoteAddr());
return new ResponseEntity<Object>(list, HttpStatus.ACCEPTED);
}
댓글 ( 4)
댓글 남기기