如何使用 Java 将网页内容读入字符串?
您可以使用 Java 以多种方式读取网页内容。在这里,我们将讨论其中的三个。
使用 openStream() 方法
java.net 包中的 URL 类表示统一资源定位器,用于指向万维网上的资源(文件、目录或引用)。
此类的 openStream() 方法打开与当前对象所表示的 URL 的连接,并返回一个 InputStream 对象,您可以使用该对象从 URL 读取数据。
因此,要从网页读取数据(使用 URL 类)−
通过将所需网页的 URL 作为参数传递给其构造函数来实例化 java.net.URL 类。
调用 openStream() 方法并检索 InputStream 对象。
实例化 Scanner 类通过将上面检索到的 InputStream 对象作为参数传递。
示例
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
public class ReadingWebPage {
public static void main(String args[]) throws IOException {
//实例化 URL 类
URL url = new URL("http://www.something.com/");
//检索指定页面的内容
Scanner sc = new Scanner(url.openStream());
//实例化 StringBuffer 类以保存结果
StringBuffer sb = new StringBuffer();
while(sc.hasNext()) {
sb.append(sc.next());
//System.out.println(sc.next());
}
//从 String Buffer 对象中检索字符串
String result = sb.toString();
System.out.println(result);
//删除 HTML 标签
result = result.replaceAll("<[^>]*>", "");
System.out.println("Contents of the web page: "+result);
}
}
输出
<html><body><h1>Itworks!</h1></body></html> Contents of the web page: Itworks!
使用 HttpClient
Http 客户端是一个传输库,它位于客户端,发送和接收 HTTP 消息。它提供最新的、功能丰富的、高效的实现,符合最新的 HTTP 标准。
GET 请求(Http 协议)用于使用给定的 URI 从给定的服务器检索信息。使用 GET 的请求应该只检索数据,并且不应该对数据产生其他影响。
HttpClient API 提供了一个名为 HttpGet 的类,它代表 get 请求方法。要执行 GET 请求并检索网页的内容 −
HttpClients 类的 createDefault() 方法返回一个 CloseableHttpClient 对象,它是 HttpClient 接口的基本实现。使用此方法创建一个 HttpClient 对象。
通过实例化 HttpGet 类来创建 HTTP GET 请求。此类的构造函数接受一个字符串值,该值表示您需要向其发送请求的网页的 URI。
通过调用 execute() 方法执行 HttpGet 请求。
从响应中检索表示网站内容的 InputStream 对象作为 −
httpresponse.getEntity().getContent()
示例
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpClientExample {
public static void main(String args[]) throws Exception{
//创建 HttpClient 对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建 HttpGet 对象
HttpGet httpget = new HttpGet("http://www.something.com/");
//执行 Get 请求
HttpResponse httpresponse = httpclient.execute(httpget);
Scanner sc = new Scanner(httpresponse.getEntity().getContent());
//实例化 StringBuffer 类以保存结果
StringBuffer sb = new StringBuffer();
while(sc.hasNext()) {
sb.append(sc.next());
//System.out.println(sc.next());
}
//从字符串缓冲区对象中检索字符串
String result = sb.toString();
System.out.println(result);
//删除 HTML 标签
result = result.replaceAll("<[^>]*>", "");
System.out.println("Contents of the web page: "+result);
}
}
输出
<html><body><h1>Itworks!</h1></body></html> Contents of the web page: Itworks!
使用 Jsoup 库
Jsoup 是一个基于 Java 的库,用于处理基于 HTML 的内容。它提供了一个非常方便的 API 来提取和操作数据,使用最好的 DOM、CSS 和类似 jquery 的方法。它实现了 WHATWG HTML5 规范,并将 HTML 解析为与现代浏览器相同的 DOM。
使用 Jsoup 库检索网页内容 −
Jsoup 类的 connect() 方法接受网页的 URL 并连接到指定的网页并返回连接对象。使用 connect() 方法连接到所需的网页。
Connection 接口的 get() 方法发送/执行 GET 请求并将 HTML 文档作为 Document 类的对象返回。通过调用 get() 方法向页面发送 GET 请求。
将获取的文档的内容检索到字符串中作为 −
String result = doc.body().text();
示例
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupExample {
public static void main(String args[]) throws IOException {
String page = "http://www.something.com/";
//连接到网页
Connection conn = Jsoup.connect(page);
//执行 get 请求
Document doc = conn.get();
//检索网页内容(正文)
String result = doc.body().text();
System.out.println(result);
}
}
输出
It works!
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

