public class TestHttpClient {
public static void main(String[] args) throws ClientProtocolException, IOException {
//创建httpClient实例
CloseableHttpClient httpClient=HttpClients.createDefault();
//创建httpGet
HttpGet httpGet=new HttpGet("https://movie.douban.com/cinema/nowplaying/nanjing/");
System.out.println(httpGet.getURI());
//执行get请求并接收响应信息
CloseableHttpResponse response= httpClient.execute(httpGet);
//获取响应实体
HttpEntity entity= response.getEntity();
//打印响应状态
System.out.println(response.getStatusLine());
System.out.println("---------------------------------");
if (entity!=null) {
String web=EntityUtils.toString(entity,"utf-8");
// System.out.println("响应内容长度"+entity.getContentLength());
// System.out.println("响应内容"+web);
Document doc= Jsoup.parse(web);
//获取排行榜标题
Elements title =doc.getElementsByClass("movie_ranking_time");
for (Element element : title) {
System.out.println(element.text());
}
//获取排行榜类型
Elements name =doc.select("ul.tab_nav>li");
//获取排行榜
Elements content =doc.select("ul.view_1");
for (int i = 0; i < name.size(); i++) {
System.out.println(name.get(i).text());
Elements movie=content.get(i).select("li");
for (int j = 0; j < movie.size(); j++) {
System.out.println(movie.get(j).text());
}
}
}
System.out.println("---------------------------------");
response.close();
httpClient.close();
}
}