執行個體為從我文章中讀取标題。

package com.test.test;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class WebHtmlTest {
public static void main(String[] args) throws IOException {
/*
作用:從url中讀取web頁面的内容
*/
String html_url = "https://lanzao.blog.csdn.net/article/details/119329989";
// 連接配接的逾時時間
System.setProperty("sun.net.client.defaultConnectTimeout", "20000");
// 讀取資料的逾時時間
System.setProperty("sun.net.client.defaultReadTimeout", "20000");
try {
URL url = new URL(html_url);
URLConnection url_connection = url.openConnection();
InputStream input_stream = url_connection.getInputStream();
InputStreamReader input_stream_reader = new InputStreamReader(input_stream,"utf-8");
BufferedReader html_reader = new BufferedReader(input_stream_reader);
String html_reader_line = null;
// 讀取html内容
while ((html_reader_line = html_reader.readLine()) != null) {
if(html_reader_line.contains("class=\"title-article\"")) {
System.out.println(html_reader_line);
}
}
// 關閉建立的對象
html_reader.close();
input_stream_reader.close();
input_stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
喜歡的點個贊❤吧!