Home> Java> javaTutorial> body text

Java implementation case of reading web page content

黄舟
Release: 2017-09-26 09:16:20
Original
1758 people have browsed it

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) { } } }
Copy after login

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(...))
Copy after login

InputStreamReader.

The input source here should be:


(new URL(url).openConnection().getInputStream(), "GB2312")
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!