您可以使用Java以多种方式阅读网页的内容。在这里,我们将讨论其中的三个。
openStream()方法java.net包的URL类表示一个统一资源定位符,用于在万维网上指向资源(文件,目录或引用)。
OpenStream()此类的方法打开到URL的连接表示由当前对象,并返回使用它可以读取来自URL数据的InputStream对象。
因此,要从网页读取数据(使用URL类)-
通过将所需网页的URL作为参数传递给其构造函数来实例化java.net.URL类。
调用该openStream()方法并检索InputStream对象。
通过传递上面获取的InputStream对象作为参数来实例化Scanner类。
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 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!
Http客户端是一个传输库,它位于客户端,用于发送和接收HTTP消息。它提供了符合最新HTTP标准的最新,功能丰富且高效的实现。
(Http协议的)GET请求用于使用给定URI从给定服务器检索信息。使用GET的请求应仅检索数据,而对数据没有其他影响。
HttpClient API提供了一个名为HttpGet的类,该类表示get请求方法。执行GET请求并检索网页内容-
HttpClients类的createDefault()方法返回一个CloseableHttpClient对象,该对象是HttpClient接口的基本实现。使用此方法,创建一个HttpClient对象。
通过实例化HttpGet类创建一个HTTP GET请求。此类的构造函数接受一个String值,该值表示您需要向其发送请求的网页的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/");
//执行获取请求
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是一个基于Java的库,可以处理基于HTML的内容。它提供了一个非常方便的API,可以使用DOM,CSS和类似jquery的最佳方法来提取和处理数据。它实现WHATWG HTML5规范,并将HTML解析为与现代浏览器相同的DOM。
使用Jsoup库检索网页的内容-
Jsoup类的connect()方法接受网页的URL并连接到指定的网页并返回连接对象。使用connect()方法连接到所需的网页。
get()Connection接口的方法发送/执行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);
//执行获取请求
Document doc = conn.get();
//检索网页的内容(正文)
String result = doc.body().text();
System.out.println(result);
}
}It works!