实例为从我文章中读取标题。
通过class属性锁定标题元素,把匹配的内容打印出来。
下面是源码:
package com.test.test;import java.io.*;import .URL;import .URLConnection;public class WebHtmlTest {public static void main(String[] args) throws IOException {/*作用:从url中读取web页面的内容*/String html_url = "https://lanzao./article/details/119329989";// 连接的超时时间System.setProperty(".client.defaultConnectTimeout", "20000");// 读取数据的超时时间System.setProperty(".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();}}}
喜欢的点个赞❤吧!
Java 技术篇 - 从指定的web网页页面中读取html内容实例演示 从http协议下的url地址中读取web页面内容方法