英文:
How to get html code while hitting one website using spring boot and store this whole HTML data in one string variable?
问题
我试图找一些关于如何使用Spring Boot在访问任何网站时获取HTML数据的资料,但是我没有找到任何最佳示例。有谁可以帮助我提供解决方案吗?
英文:
I tried to find some stuffs regarding how to get HTML data while hitting any of the website using spring boot but I didn't get any of the best example stuffs.Can anyone help me to give solution for this?
答案1
得分: 0
你可以使用HTML解析器,例如JSoup
来完成这个任务。
演示:
import java.io.IOException;
import org.jsoup.Jsoup;
public class JSoupDemo {
public static void main(String[] args) throws IOException {
String webPage = "http://www.example.com";
String html = Jsoup.connect(webPage).get().html();
System.out.println(html);
}
}
输出:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
**或者,**你也可以使用java.io.BufferedReader
来完成,如下所示:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new URL("http://www.example.com").openStream()))) {
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
英文:
You can use an HTML parser e.g. JSoup
to do it.
Demo:
import java.io.IOException;
import org.jsoup.Jsoup;
public class JSoupDemo {
public static void main(String[] args) throws IOException {
String webPage = "http://www.example.com";
String html = Jsoup.connect(webPage).get().html();
System.out.println(html);
}
}
Output:
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
Alternatively, you can do it using java.io.BufferedReader
as follows:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(new URL("http://www.example.com").openStream()))) {
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论