java-HttpClient-之豆瓣电影排行榜
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-HttpClient-之豆瓣电影排行榜相关的知识,希望对你有一定的参考价值。
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();
}
}
以上是关于java-HttpClient-之豆瓣电影排行榜的主要内容,如果未能解决你的问题,请参考以下文章
python爬虫实例教程之豆瓣电影排行榜--python爬虫requests库
Python爬虫编程思想(87):项目实战--抓取豆瓣电影排行榜