Spring Boot搭建Web项目
环境
JDK 1.8
IntelliJ IDEA 2020.3.2 (Ultimate Edition)
PyCharm 2020.2.4 (Professional Edition)
Maven 3.6
bs4==0.0.1
urllib3==1.24.2
MongoDB v4.4.4
Python 3.7.4
pymongo 3.11.3
bootstrap@4.6.0
Maven配置文件
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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring.boot.mongodb</groupId>
<artifactId>mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mongodb</name>
<description>MongoDB 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-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Spring Boot配置文件
src/main/resources/application.yaml
MongoDB默认端口
27017
,
douban
为数据库名
spring:
data:
mongodb:
uri: mongodb://localhost:27017/douban
获取数据
import urllib
from bs4 import BeautifulSoup
import re
import pymongo
def crawl():
url = "https://movie.douban.com/top250?start="
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"}
image_url_pattern = re.compile(r'<img .*src="(.*?)".*>')
title_pattern = re.compile(r'<span class="title">(.*?)</span>')
information_pattern = re.compile(r'<p class="">(.*?)</p>', re.S)
rating_pattern = re.compile(r'<span class="rating_num" property="v:average">(.*?)</span>')
evaluator_pattern = re.compile(r'<span>(.*?)人评价</span>')
synopsis_pattern = re.compile(r'<span class="inq">(.*?)</span>')
movies = []
for i in range(0, 10):
request = urllib.request.Request(url=url + str(25 * i), headers=headers)
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
beautiful_soup = BeautifulSoup(html, "html.parser")
for item in beautiful_soup.find_all('div', class_='item'):
movie = {}
item = str(item)
image_url = re.findall(image_url_pattern, item)[0]
movie["image_url"] = image_url
title = re.findall(title_pattern, item)[0]
movie["title"] = title
information = re.findall(information_pattern, item)
information_list = information[0].strip().replace('\xa0', ',').split(',')
movie["country"] = information_list[-3]
movie["category"] = information_list[-1]
rating = re.findall(rating_pattern, item)[0]
movie["rating"] = rating
evaluator = re.findall(evaluator_pattern, item)[0]
movie["evaluator"] = evaluator
synopsis = re.findall(synopsis_pattern, item)
if len(synopsis) > 0:
movie["synopsis"] = synopsis[0]
else:
movie["synopsis"] = "None"
movies.append(movie)
return movies
def save_in_mongodb(uri, database_name, collection_name, data):
mongo_client = pymongo.MongoClient(uri)
database = mongo_client[database_name]
collection = database[collection_name]
collection.insert_many(data)
if __name__ == "__main__":
movies = crawl()
save_in_mongodb('mongodb://localhost:27017', 'douban', 'movies', movies)
实现代码
MongodbApplication类
src/main/java/com/spring/boot/mybatis/MybatisApplication.java
package com.spring.boot.mongodb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication()
public class MongodbApplication {
public static void main(String[] args) {
SpringApplication.run(MongodbApplication.class, args);
}
}
Movie类
src/main/java/com/spring/boot/mongodb/bean/Movie.java
package com.spring.boot.mongodb.bean;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "movies")
public class Movie {
@Id
private String id;
@Field("image_url")
private String imageURL;
private String title;
private String country;
private String category;
private double rating;
private int evaluator;
private String synopsis;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
public int getEvaluator() {
return evaluator;
}
public void setEvaluator(int evaluator) {
this.evaluator = evaluator;
}
public String getSynopsis() {
return synopsis;
}
public void setSynopsis(String synopsis) {
this.synopsis = synopsis;
}
@Override
public String toString() {
return "Movie{" +
"id='" + id + '\'' +
", imageURL='" + imageURL + '\'' +
", title='" + title + '\'' +
", country='" + country + '\'' +
", rating=" + rating +
", evaluator=" + evaluator +
", synopsis='" + synopsis + '\'' +
'}';
}
}
HomePageController类
src/main/java/com/spring/boot/mongodb/controller/HomePageController.java
package com.spring.boot.mongodb.controller;
import com.spring.boot.mongodb.bean.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class HomePageController {
@Autowired
private MongoTemplate mongoTemplate;
@GetMapping("/")
public String HomePage(Model model) {
Query query = new Query(new Criteria());
List<Movie> movies = mongoTemplate.find(query, Movie.class);
model.addAttribute("movies", movies);
return "index";
}
}
H5模板
src/main/resources/templates/index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Movies</title>
</head>
<body>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Synopsis</th>
<th scope="col">Rating</th>
<th scope="col">Country</th>
<th scope="col">Category</th>
</tr>
</thead>
<tbody>
<tr th:scope="row" th:each="movie, status:${movies}">
<td th:text="${status.count}"/>
<td th:text="${movie.title}"/>
<td th:text="${movie.synopsis}"/>
<td th:text="${movie.rating}"/>
<td th:text="${movie.country}"/>
<td th:text="${movie.category}"/>
</tr>
</tbody>
</table>
<!-- CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<!-- jQuery and JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-LCPyFKQyML7mqtS+4XytolfqyqSlcbB3bvDuH9vX2sdQMxRonb/M3b9EmhCNNNrV" crossorigin="anonymous"></script>
</body>
</html>
测试结果
http://127.0.0.1:8080/
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
版权声明:本文为qq_44486439原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。