Feat: 从指定 url 获取 HTML 文件
This commit is contained in:
@@ -11,13 +11,36 @@
|
|||||||
<java.version>21</java.version>
|
<java.version>21</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.2.2</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
|
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jsoup</groupId>
|
<groupId>org.jsoup</groupId>
|
||||||
<artifactId>jsoup</artifactId>
|
<artifactId>jsoup</artifactId>
|
||||||
<version>1.17.2</version>
|
<version>1.17.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-http -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-http</artifactId>
|
||||||
|
<version>5.8.31</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,18 +1,9 @@
|
|||||||
package cn.hezhaohui.fund;
|
package cn.hezhaohui.fund;
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String htmlString = "<html><head><title>My title</title></head>"
|
SpringApplication.run(Application.class, args);
|
||||||
+ "<body>Body content</body></html>";
|
|
||||||
|
|
||||||
Document doc = Jsoup.parse(htmlString);
|
|
||||||
String title = doc.title();
|
|
||||||
String body = doc.body().text();
|
|
||||||
|
|
||||||
System.out.printf("Title: %s%n", title);
|
|
||||||
System.out.printf("Body: %s", body);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.hezhaohui.fund.util;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class WebUtil {
|
||||||
|
public static String getContentByUrl(String url) {
|
||||||
|
String string = HttpUtil.get(url);
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package cn.hezhaohui.fund.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class WebUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getContentByUrl() {
|
||||||
|
String content = WebUtil.getContentByUrl("https://fund.eastmoney.com/001068.html");
|
||||||
|
System.out.println(content);
|
||||||
|
Assert.notNull(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user