欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java爬取豆瓣電影數(shù)據(jù)的方法詳解

 更新時間:2020年04月05日 12:01:26   作者:一枕江風(fēng)  
這篇文章主要介紹了Java爬取豆瓣電影數(shù)據(jù)的方法,結(jié)合實例形式詳細(xì)分析了Java爬取豆瓣電影數(shù)據(jù)相關(guān)原理、操作步驟、實現(xiàn)技巧與注意事項,需要的朋友可以參考下

本文實例講述了Java爬取豆瓣電影數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

所用到的技術(shù)有Jsoup,HttpClient。

Jsoup

jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內(nèi)容。它提供了一套非常省力的API,可通過DOM,CSS以及類似于jQuery的操作方法來取出和操作數(shù)據(jù)。

HttpClient

HTTP 協(xié)議可能是現(xiàn)在 Internet 上使用得最多、最重要的協(xié)議了,越來越多的 Java 應(yīng)用程序需要直接通過 HTTP 協(xié)議來訪問網(wǎng)絡(luò)資源。雖然在 JDK 的 java net包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能,但是對于大部分應(yīng)用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活。HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。

爬取豆瓣電影數(shù)據(jù)

豆瓣電影網(wǎng)址。

https://movie.douban.com/explore#!type=movie&tag=熱門&sort=recommend&page_limit=20&page_start=0

打開瀏覽器f12,地址欄中輸入該地址訪問,可以看到請求響應(yīng)的頁面,對應(yīng)可以找到電影數(shù)據(jù)的請求地址,數(shù)據(jù)請求地址

https://movie.douban.com/j/search_subjects?type=movie&tag=熱門&sort=recommend&page_limit=20&page_start=0

可以看到數(shù)據(jù)請求地址響應(yīng)過來的是一個JSON格式的數(shù)據(jù),之后我們看到請求地址上的參數(shù)type=movie&tag=熱門&sort=recommend&page_limit=20&page_start=0。其中type是電影tag是標(biāo)簽,sort是按照熱門進(jìn)行排序的,page_limit是每頁20條數(shù)據(jù),page_start是從第幾條數(shù)據(jù)開始查詢(下標(biāo)從0開始)。但是這不是我們想要的,我們需要去找豆瓣電影數(shù)據(jù)的總?cè)肟诘刂肥窍旅孢@個

https://movie.douban.com/j/search_subjects

創(chuàng)建SpringBoot項目爬取數(shù)據(jù)

把爬取到的數(shù)據(jù)保存到數(shù)據(jù)庫中,電影圖片保存在本地磁盤中,這里持久層用的是JPA,所以需要引入對應(yīng)的依賴。pom.xml中依賴代碼如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.mcy</groupId>
  <artifactId>crawler-douban</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>crawler-douban</name>
  <description>Demo project for Spring Boot</description>
 
  <properties>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!--httpclient-->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.2</version>
    </dependency>
    <!--jsoup,解析HTML-->
    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.11.3</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
 
</project>

項目目錄結(jié)構(gòu)如下。

首先我們在entity包中建立實體對象,字段為豆瓣電影的基本信息(有些信息是詳情頁面的信息)。

Movie實體類。

import javax.persistence.*;
 
@Entity
public class Movie {
  private Integer id;
  private double rate;  //評分
  private String title;  //電影名稱
  private String director;  //導(dǎo)演
  private String protagonist; //主演
  private String dateTime;  //電影時長
 
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public Integer getId() {
    return id;
  }
 
  public void setId(Integer id) {
    this.id = id;
  }
 
  public double getRate() {
    return rate;
  }
 
  public void setRate(double rate) {
    this.rate = rate;
  }
 
  public String getTitle() {
    return title;
  }
 
  public void setTitle(String title) {
    this.title = title;
  }
 
  public String getDirector() {
    return director;
  }
 
  public void setDirector(String director) {
    this.director = director;
  }
 
  @Column(length=2000)
  public String getProtagonist() {
    return protagonist;
  }
 
  public void setProtagonist(String protagonist) {
    this.protagonist = protagonist;
  }
 
  public String getDateTime() {
    return dateTime;
  }
 
  public void setDateTime(String dateTime) {
    this.dateTime = dateTime;
  }
}

在src/main/resources下找到application.properties文件,在該配置文件中配置數(shù)據(jù)庫鏈接信息,需要在數(shù)據(jù)庫中新建一個名為douban的數(shù)據(jù)庫。

spring.datasource.url=jdbc:mysql://localhost:3306/douban?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.use-new-id-generator-mappings=false

創(chuàng)建MovieRepository數(shù)據(jù)訪問層接口

import com.mcy.crawlerdouban.entity.Movie;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface MovieRepository extends JpaRepository<Movie, Integer> {
 
}

創(chuàng)建MovieService類,里邊有一個保存數(shù)據(jù)的方法。

import com.mcy.crawlerdouban.entity.Movie;
import com.mcy.crawlerdouban.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service
public class MovieService {
  @Autowired
  private MovieRepository movieRepository;
 
  public void save(Movie movie) {
    movieRepository.save(movie);
  }
}

創(chuàng)建一個HttpUtils獲取網(wǎng)頁數(shù)據(jù)和保存圖片的工具類。

創(chuàng)建連接池和配置連接池信息。

//創(chuàng)建連接池管理器
private static PoolingHttpClientConnectionManager cm;
 
public HttpUtils(){
  cm = new PoolingHttpClientConnectionManager();
  //設(shè)置最大連接數(shù)
  cm.setMaxTotal(100);
  //設(shè)置每個主機(jī)的最大連接數(shù)
  cm.setDefaultMaxPerRoute(10);
}
 
//配置請求信息
private static RequestConfig getConfig() {
  RequestConfig config = RequestConfig.custom()
      .setConnectTimeout(10000)   //創(chuàng)建連接的最長時間,單位毫秒
      .setConnectionRequestTimeout(10000) //設(shè)置獲取鏈接的最長時間,單位毫秒
      .setSocketTimeout(10000)   //設(shè)置數(shù)據(jù)傳輸?shù)淖铋L時間,單位毫秒
      .build();
  return config;
}

根據(jù)請求地址獲取響應(yīng)信息方法,獲取成功后返回響應(yīng)信息。

public static String doGetHtml(String url, Map<String, String> map, Map<String, String> mapTile) throws URISyntaxException {
  //創(chuàng)建HTTPClient對象
  CloseableHttpClient httpClient = HttpClients.createDefault();
 
  //設(shè)置請求地址
  //創(chuàng)建URLBuilder
  URIBuilder uriBuilder = new URIBuilder(url);
 
  //設(shè)置參數(shù)
  if(!map.isEmpty()){
    for(String key : map.keySet()){
      uriBuilder.setParameter(key, map.get(key));
    }
  }
 
  //創(chuàng)建HTTPGet對象,設(shè)置url訪問地址
  //uriBuilder.build()得到請求地址
  HttpGet httpGet = new HttpGet(uriBuilder.build());
 
  //設(shè)置請求頭信息
  if(!mapTile.isEmpty()){
    for(String key : mapTile.keySet()){
      httpGet.addHeader(key, mapTile.get(key));
    }
  }
 
  //設(shè)置請求信息
  httpGet.setConfig(getConfig());
  System.out.println("發(fā)起請求的信息:"+httpGet);
 
  //使用HTTPClient發(fā)起請求,獲取response
  CloseableHttpResponse response = null;
  try {
    response = httpClient.execute(httpGet);
    //解析響應(yīng)
    if(response.getStatusLine().getStatusCode() == 200){
      //判斷響應(yīng)體Entity是否不為空,如果不為空就可以使用EntityUtils
      if(response.getEntity() != null) {
        String content = EntityUtils.toString(response.getEntity(), "utf8");
        return content;
      }
    }
  }catch (IOException e){
    e.printStackTrace();
  }finally {
    //關(guān)閉response
    try {
      response.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  return "";
}

根據(jù)鏈接下載圖片保存到本地方法。

public static String doGetImage(String url) throws IOException {
  //獲取HTTPClient對象
  CloseableHttpClient httpClient = HttpClients.createDefault();
  //設(shè)置HTTPGet請求對象,設(shè)置url地址
  HttpGet httpGet = new HttpGet(url);
  //設(shè)置請求信息
  httpGet.setConfig(getConfig());
  //使用HTTPClient發(fā)起請求,獲取響應(yīng)
  CloseableHttpResponse response = null;
  try {
    //使用HTTPClient發(fā)起請求,獲取響應(yīng)
    response = httpClient.execute(httpGet);
    //解析響應(yīng),返回結(jié)果
    if(response.getStatusLine().getStatusCode() == 200){
      //判斷響應(yīng)體Entity是否不為空
      if(response.getEntity() != null) {
        //下載圖片
        //獲取圖片的后綴
        String extName = url.substring(url.lastIndexOf("."));
        //創(chuàng)建圖片名,重命名圖片
        String picName = UUID.randomUUID().toString() + extName;
        //下載圖片
        //聲明OutputStream
        OutputStream outputStream = new FileOutputStream(new File("E://imges/" + picName));
        response.getEntity().writeTo(outputStream);
        //返回圖片名稱
        return picName;
      }
    }
  } catch (IOException e) {
    e.printStackTrace();
  }finally {
    //關(guān)閉response
    if(response != null){
      try {
        response.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  return "";
}

HttpUtils工具類全部代碼。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.UUID;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
 
public class HttpUtils {
 
  //創(chuàng)建連接池管理器
  private static PoolingHttpClientConnectionManager cm;
 
  public HttpUtils(){
    cm = new PoolingHttpClientConnectionManager();
    //設(shè)置最大連接數(shù)
    cm.setMaxTotal(100);
    //設(shè)置每個主機(jī)的最大連接數(shù)
    cm.setDefaultMaxPerRoute(10);
  }
 
  //配置請求信息
  private static RequestConfig getConfig() {
    RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(10000)   //創(chuàng)建連接的最長時間,單位毫秒
        .setConnectionRequestTimeout(10000) //設(shè)置獲取鏈接的最長時間,單位毫秒
        .setSocketTimeout(10000)   //設(shè)置數(shù)據(jù)傳輸?shù)淖铋L時間,單位毫秒
        .build();
    return config;
  }
 
  /**
   * 根據(jù)請求地址下載頁面數(shù)據(jù)
   * @param url  請求路徑
   * @param map  請求參數(shù)
   * @param mapTile  請求頭
   * @return //頁面數(shù)據(jù)
   * @throws URISyntaxException
   */
  public static String doGetHtml(String url, Map<String, String> map, Map<String, String> mapTile) throws URISyntaxException {
    //創(chuàng)建HTTPClient對象
    CloseableHttpClient httpClient = HttpClients.createDefault();
 
    //設(shè)置請求地址
    //創(chuàng)建URLBuilder
    URIBuilder uriBuilder = new URIBuilder(url);
 
    //設(shè)置參數(shù)
    if(!map.isEmpty()){
      for(String key : map.keySet()){
        uriBuilder.setParameter(key, map.get(key));
      }
    }
 
    //創(chuàng)建HTTPGet對象,設(shè)置url訪問地址
    //uriBuilder.build()得到請求地址
    HttpGet httpGet = new HttpGet(uriBuilder.build());
 
    //設(shè)置請求頭信息
    if(!mapTile.isEmpty()){
      for(String key : mapTile.keySet()){
        httpGet.addHeader(key, mapTile.get(key));
      }
    }
 
    //設(shè)置請求信息
    httpGet.setConfig(getConfig());
    System.out.println("發(fā)起請求的信息:"+httpGet);
 
    //使用HTTPClient發(fā)起請求,獲取response
    CloseableHttpResponse response = null;
    try {
      response = httpClient.execute(httpGet);
      //解析響應(yīng)
      if(response.getStatusLine().getStatusCode() == 200){
        //判斷響應(yīng)體Entity是否不為空,如果不為空就可以使用EntityUtils
        if(response.getEntity() != null) {
          String content = EntityUtils.toString(response.getEntity(), "utf8");
          return content;
        }
      }
    }catch (IOException e){
      e.printStackTrace();
    }finally {
      //關(guān)閉response
      try {
        response.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return "";
  }
 
  /**
   * 下載圖片
   * @param url
   * @return 圖片名稱
   */
  public static String doGetImage(String url) throws IOException {
    //獲取HTTPClient對象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    //設(shè)置HTTPGet請求對象,設(shè)置url地址
    HttpGet httpGet = new HttpGet(url);
    //設(shè)置請求信息
    httpGet.setConfig(getConfig());
    //使用HTTPClient發(fā)起請求,獲取響應(yīng)
    CloseableHttpResponse response = null;
    try {
      //使用HTTPClient發(fā)起請求,獲取響應(yīng)
      response = httpClient.execute(httpGet);
      //解析響應(yīng),返回結(jié)果
      if(response.getStatusLine().getStatusCode() == 200){
        //判斷響應(yīng)體Entity是否不為空
        if(response.getEntity() != null) {
          //下載圖片
          //獲取圖片的后綴
          String extName = url.substring(url.lastIndexOf("."));
          //創(chuàng)建圖片名,重命名圖片
          String picName = UUID.randomUUID().toString() + extName;
          //下載圖片
          //聲明OutputStream
          OutputStream outputStream = new FileOutputStream(new File("E://imges/" + picName));
          response.getEntity().writeTo(outputStream);
          //返回圖片名稱
          return picName;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      //關(guān)閉response
      if(response != null){
        try {
          response.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return "";
  }
}

在項目的test類中編寫代碼獲取數(shù)據(jù)保存到數(shù)據(jù)庫中。

先通過@Resource注解將MovieService類對應(yīng)的實現(xiàn)類注入進(jìn)來。

@Autowired
private MovieService movieService;

設(shè)置請求地址https://movie.douban.com/j/search_subjects

String url = "https://movie.douban.com/j/search_subjects";

之后在定義兩個Map,用于存儲請求頭和請求參數(shù)信息。

網(wǎng)頁請求頭。

請求參數(shù),type=movie&tag=熱門&sort=recommend&page_limit=20&page_start=0

設(shè)置請求參數(shù)和請求頭代碼如下。

Map<String, String> map = new HashMap<>();
Map<String, String> mapTitle = new HashMap<>();
//設(shè)置請求參數(shù)
map.put("type", "movie");
map.put("tag", "熱門");
map.put("sort", "recommend");
map.put("page_limit", "20");
//i為一個變量,從多少條數(shù)據(jù)開始查詢
map.put("page_start", i+"");
//設(shè)置請求頭
mapTitle.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
mapTitle.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0");
mapTitle.put("Cookie", "bid=QNoG_zn4mZY; _pk_id.100001.4cf6=6209709719896af7.1575619506.2.1575940374.1575621362.; __utma=30149280.1889677372.1575619507.1575619507.1575940335.2; __utmz=30149280.1575619507.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.986359939.1575619507.1575619507.1575940335.2; __utmz=223695111.1575619507.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __yadk_uid=QVSP2uvzzDBrpnvHKzZpZEWJnuARZ4aL; ll=\"118259\"; _vwo_uuid_v2=D1FC45CAE50CF6EE38D245C68D7CECC4F|e8d1db73f4c914f0b0be7ed85ac50d14; trc_cookie_storage=taboola%2520global%253Auser-id%3D690a21c0-9ad9-4f8d-b997-f0decb3cfc9b-tuct4e39874; _pk_ses.100001.4cf6=*; ap_v=0,6.0; __utmb=30149280.0.10.1575940335; __utmc=30149280; __utmb=223695111.0.10.1575940335; __utmc=223695111; __gads=ID=2f06cb0af40206d0:T=1575940336:S=ALNI_Ma4rv9YmqrkIUNXsIt5E7zT6kZy2w");

通過HttpUtils類doGetHtml方法獲取該請求響應(yīng)的數(shù)據(jù)。

String html = HttpUtils.doGetHtml(url, map, mapTitle);

請求響應(yīng)數(shù)據(jù)格式。

可以看出是一個json格式的數(shù)據(jù),我們可以通過阿里巴巴的Fastjson一個json解析庫,把它解析成為一個List格式數(shù)據(jù)。Fastjson基本用法

JSONObject jsonObject = JSONObject.parseObject(html);
JSONArray jsonArray = jsonObject.getJSONArray("subjects");

因為每頁查詢是是20條數(shù)據(jù),我們用一個for循環(huán)遍歷一下這一頁的數(shù)據(jù)。可以獲得電影的標(biāo)題,評分,圖片鏈接和詳情頁面的鏈接,上面JSON數(shù)據(jù)中的cover屬性值為圖片的地址。通過圖片的鏈接我們可以調(diào)用HttpUtils類的doGetImage方法把圖片保存到本地磁盤。

HttpUtils.doGetImage(json.getString("cover"));

上面請求的數(shù)據(jù)只能獲取到標(biāo)題,評分和圖片,然而我們還有獲取導(dǎo)演,主演,和電影時長。這些信息我們點開上面請求到的json數(shù)據(jù)的url屬性值,會打開詳情頁面,詳情頁面中有導(dǎo)演,主演,和電影時長信息。

打開的詳情頁面,我們可以看到導(dǎo)演,主演和電影時長等信息。

我們查詢詳情頁面的源代碼,可以看到導(dǎo)演,主演,電影時長等信息的位置。

我們在通過HttpUtils類doGetHtml方法獲取詳情頁面的數(shù)據(jù),利用Jsoup進(jìn)行解析,Jsoup是一個可以讓java代碼解析HTML代碼的一個工具,可以參考一下Jsoup官網(wǎng)文檔,找到主演,導(dǎo)演和電影時長信息。到這里我們需要的全部信息都獲取到了,最后把數(shù)據(jù)保存起來。

String url2 = json.getString("url");
Map<String, String> map2 = new HashMap<>();
Map<String, String> mapTitle2 = new HashMap<>();
String html2 = HttpUtils.doGetHtml(url2, map2, mapTitle2);
//解析HTML獲取DOM對象
Document doc = Jsoup.parse(html2);
//獲取導(dǎo)演名稱
Element element = doc.select("div#info a[rel=v:directedBy]").first();
movie.setDirector(element.text());
Elements elements = doc.select("div#info a[rel=v:starring]");
//主演
String protagonist = "";
for (Element e : elements) {
  protagonist += e.text()+",";
}
if(!protagonist.equals("")){
  protagonist = protagonist.substring(0, protagonist.length()-1);
}
movie.setProtagonist(protagonist);
//獲取電影時長
element = doc.select("div#info span[property=v:runtime]").first();
movie.setDateTime(element.text());
movieService.save(movie);

測試類全部代碼如下。

import com.alibaba.fastjson.JSONObject;
import com.mcy.crawlerdouban.entity.Movie;
import com.mcy.crawlerdouban.service.MovieService;
import com.mcy.crawlerdouban.util.HttpUtils;
import com.alibaba.fastjson.JSONArray;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
 
@SpringBootTest
class CrawlerDoubanApplicationTests {
 
  @Autowired
  private MovieService movieService;
 
  @Test
  public void contextLoads() throws URISyntaxException, IOException {
    //請求地址
    //https://movie.douban.com/j/search_subjects?type=movie&tag=熱門&sort=recommend&page_limit=20&page_start=0
    String url = "https://movie.douban.com/j/search_subjects";
    Map<String, String> map = new HashMap<>();
    Map<String, String> mapTitle = new HashMap<>();
    //設(shè)置請求參數(shù)
    map.put("type", "movie");
    map.put("tag", "熱門");
    map.put("sort", "recommend");
    map.put("page_limit", "20");
    //設(shè)置請求頭
    mapTitle.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    mapTitle.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0");
    mapTitle.put("Cookie", "bid=QNoG_zn4mZY; _pk_id.100001.4cf6=6209709719896af7.1575619506.2.1575940374.1575621362.; __utma=30149280.1889677372.1575619507.1575619507.1575940335.2; __utmz=30149280.1575619507.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.986359939.1575619507.1575619507.1575940335.2; __utmz=223695111.1575619507.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __yadk_uid=QVSP2uvzzDBrpnvHKzZpZEWJnuARZ4aL; ll=\"118259\"; _vwo_uuid_v2=D1FC45CAE50CF6EE38D245C68D7CECC4F|e8d1db73f4c914f0b0be7ed85ac50d14; trc_cookie_storage=taboola%2520global%253Auser-id%3D690a21c0-9ad9-4f8d-b997-f0decb3cfc9b-tuct4e39874; _pk_ses.100001.4cf6=*; ap_v=0,6.0; __utmb=30149280.0.10.1575940335; __utmc=30149280; __utmb=223695111.0.10.1575940335; __utmc=223695111; __gads=ID=2f06cb0af40206d0:T=1575940336:S=ALNI_Ma4rv9YmqrkIUNXsIt5E7zT6kZy2w");
    //獲取前100條數(shù)據(jù),可以自行更改
    for(int i = 0; i < 100; i+=20){
      map.put("page_start", i+"");
      String html = HttpUtils.doGetHtml(url, map, mapTitle);
      JSONObject jsonObject = JSONObject.parseObject(html);
      JSONArray jsonArray = jsonObject.getJSONArray("subjects");
      for(int j = 0; j < jsonArray.size(); j++){ //循環(huán)遍歷每頁數(shù)據(jù)
        Movie movie = new Movie();
        JSONObject json = (JSONObject) jsonArray.get(j);
        movie.setRate(json.getDouble("rate"));
        movie.setTitle(json.getString("title"));
 
        //下載保存圖片
        HttpUtils.doGetImage(json.getString("cover"));
 
        String url2 = json.getString("url");
        Map<String, String> map2 = new HashMap<>();
        Map<String, String> mapTitle2 = new HashMap<>();
        String html2 = HttpUtils.doGetHtml(url2, map2, mapTitle2);
        //解析HTML獲取DOM對象
        Document doc = Jsoup.parse(html2);
        //獲取導(dǎo)演名稱
        Element element = doc.select("div#info a[rel=v:directedBy]").first();
        movie.setDirector(element.text());
        Elements elements = doc.select("div#info a[rel=v:starring]");
        //主演
        String protagonist = "";
        for (Element e : elements) {
          protagonist += e.text()+",";
        }
        if(!protagonist.equals("")){
          protagonist = protagonist.substring(0, protagonist.length()-1);
        }
        movie.setProtagonist(protagonist);
        //獲取電影時長
        element = doc.select("div#info span[property=v:runtime]").first();
        movie.setDateTime(element.text());
        movieService.save(movie);
      }
    }
    System.out.println("數(shù)據(jù)獲取完成。。。");
  }
}

最后我們在mysql數(shù)據(jù)庫中新建一個名為douban的數(shù)據(jù)庫,啟動項目,JPA會自動在數(shù)據(jù)庫中新建一張movie表,存放獲取到的電影數(shù)據(jù)。在本地磁盤也會保存電影圖片,如圖。

電影圖片,保存的位置和HttpUtils的doGetImage方法中設(shè)置的保存地址一樣。

最后放上下載地址https://github.com/machaoyin/crawler-douban

有什么問題歡迎下方留言交流。

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java網(wǎng)絡(luò)編程技巧總結(jié)》、《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • 一文詳解SpringMVC中的@RequestMapping注解

    一文詳解SpringMVC中的@RequestMapping注解

    @RequestMapping是一個用于映射HTTP請求到處理方法的注解,在Spring框架中使用,它可以用于控制器類和處理方法上,用來指定處理不同URL路徑的請求,并定義請求的方法等,本文小編將給大家詳細(xì)的介紹一下SpringMVC中的@RequestMapping注解,需要的朋友可以參考下
    2023-08-08
  • Springboot項目升級2.2.x升至2.7.x的示例代碼

    Springboot項目升級2.2.x升至2.7.x的示例代碼

    本文主要介紹了Springboot項目升級2.2.x升至2.7.x的示例代碼,會有很多的坑,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • mybatis mapper互相引用resultMap啟動出錯的解決

    mybatis mapper互相引用resultMap啟動出錯的解決

    這篇文章主要介紹了mybatis mapper互相引用resultMap啟動出錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring中@EnableScheduling實現(xiàn)定時任務(wù)代碼實例

    Spring中@EnableScheduling實現(xiàn)定時任務(wù)代碼實例

    這篇文章主要介紹了Spring中@EnableScheduling實現(xiàn)定時任務(wù)代碼實例,@EnableScheduling 注解開啟定時任務(wù)功能,可以將多個方法寫在一個類,也可以分多個類寫,當(dāng)然也可以將方法直接寫在上面ScheddulConfig類中,需要的朋友可以參考下
    2024-01-01
  • 在Java中輕松使用工廠設(shè)計模式介紹

    在Java中輕松使用工廠設(shè)計模式介紹

    這篇文章主要介紹了在Java中輕松使用工廠設(shè)計模式介紹,工廠設(shè)計模式或工廠方法設(shè)計模式是一種廣泛使用且易于理解的設(shè)計模式,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,感興趣的朋友可以參考一下
    2022-09-09
  • 解決Maven不能下載Aspose的問題

    解決Maven不能下載Aspose的問題

    這篇文章主要介紹了解決Maven不能下載Aspose的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java使用注解和反射簡化編程的方法示例

    Java使用注解和反射簡化編程的方法示例

    這篇文章主要介紹了Java使用注解和反射簡化編程的方法,結(jié)合實例形式分析了java使用注解和反射調(diào)用大量函數(shù)簡化編程的相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • SpringBoot多數(shù)據(jù)源配置并通過注解實現(xiàn)動態(tài)切換數(shù)據(jù)源

    SpringBoot多數(shù)據(jù)源配置并通過注解實現(xiàn)動態(tài)切換數(shù)據(jù)源

    本文主要介紹了SpringBoot多數(shù)據(jù)源配置并通過注解實現(xiàn)動態(tài)切換數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • js+java實現(xiàn)登錄滑動圖片驗證

    js+java實現(xiàn)登錄滑動圖片驗證

    這篇文章主要為大家詳細(xì)介紹了js+java實現(xiàn)登錄滑動圖片驗證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Jenkins自動部署SpringBoot項目實踐教程

    Jenkins自動部署SpringBoot項目實踐教程

    這篇文章主要介紹了Jenkins自動部署SpringBoot項目實踐教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論