文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java Spring boot + spring-boot-starter-thymeleaf  


Pom.xml:

<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 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.anan</groupId>

<artifactId>music</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>music Maven Webapp</name>

<url>http://maven.apache.org</url>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.jsoup</groupId>

<artifactId>jsoup</artifactId>

<version>1.7.2</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

</dependencies>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.4.1.RELEASE</version>

</parent>

<build>

<finalName>music</finalName>

</build>

</project>


package music;


import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import org.jsoup.Connection;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;


public class HtmlFetcher {

public static List<HashMap<String, String>> fetcher(String url) {

System.out.println(url);

List<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();

if (url.equals("")) {

return mList;

}

try {

Connection.Response response = Jsoup.connect(url).timeout(3000).execute();

// System.out.println(response.body());

Document document = Jsoup.parse(response.body(), "utf-8");

String title = document.title();

System.out.println(title);

Elements links = document.select("span.songlist__songname_txt > a");

for (Element link : links) {

Map<String, String> map = new HashMap<String, String>();

map.put("url", link.attr("href"));

map.put("name", link.text());

mList.add((HashMap<String, String>) map);

}

} catch (IOException e) {

e.printStackTrace();

}

return mList;

}


public static void main(String[] args) {

// TODO Auto-generated method stub

String url = "https://y.qq.com/n/yqq/singer/001fNHEf1SFEFN.html#tab=album";

List<HashMap<String, String>> musicList = fetcher(url);

System.out.println(musicList.toString());

}


}


package music; 


import java.util.HashMap;

import java.util.List;

import java.util.Map;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.ui.Model;


@SpringBootApplication

@Controller

public class Application {


@RequestMapping("/")

public String greeting(Model model) {

//return "Hello World!------";

List<HashMap<String, String>> musicList = HtmlFetcher.fetcher("https://y.qq.com/n/yqq/singer/001fNHEf1SFEFN.html#tab=album");

model.addAttribute("name", "aaaa");

model.addAttribute("musicList", musicList);

System.out.println("---------------------");

System.out.println(model.toString());

return "welcome";

}


public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"></meta>

<title>热门歌曲列表</title>

</head>

<body>

<h1>QQ热门歌曲列表</h1>

    <table>

        <thead>

            <tr>

                <th>歌曲专辑</th>

                <th>URL</th>

            </tr>

        </thead>

        <tbody>

            <tr th:each="model : ${musicList}">

                <td th:text="${model.name}"></td>

                <td><a th:text="${model.url}" th:href="@{'http:' + ${model.url}}"></a></td>

            </tr>

        </tbody>

    </table>


</body>

</html>