This article mainly introduces relevant information about Java's detailed examples of reading web page content. I hope this article can help everyone learn and understand this part of the content. Friends in need can refer to it
java Detailed example of reading web page content
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.*; public class loadurl { public static void main(String args[]) { String a = null; try { String url = "(这里替换成任意网页的网址)"; BufferedReader in = new BufferedReader(new InputStreamReader( new URL(url).openConnection().getInputStream(), "GB2312"));//GB2312可以根据需要替换成要读取网页的编码 while ((a = in.readLine()) != null) { System.out.println(a); } } catch (MalformedURLException e) { } catch (IOException e) { } } }
The above code program reads the source code of a web page, including HTML and XML, into JAVA A string in String a.
The String type in Java has a large space, which can basically accommodate the content of a web page source code.
Reading content from a web page is also an operation on the input stream.
Different from the standard input source, just enter System.in in:
BufferedReader in = new BufferedReader(new InputStreamReader(...))
InputStreamReader.
The input source here should be:
(new URL(url).openConnection().getInputStream(), "GB2312")
The subsequent operations and processing are exactly the same as loading the standard input source.
BufferedReader requires that IOException be caught in JAVA. When using the URL source, in addition to introducing the java.net.* package, MalformedURLException must also be caught.
The above is the detailed content of Java implementation case of reading web page content. For more information, please follow other related articles on the PHP Chinese website!