5
我想下载网站的html源代码来解析一些信息。我如何在Java中完成这项工作?在Java中是否有类似于WebClient.DownloadString的东西?
我想下载网站的html源代码来解析一些信息。我如何在Java中完成这项工作?在Java中是否有类似于WebClient.DownloadString的东西?
只需从openStream()返回的URL的InputStream中附加BufferedReader(或读取字符串的任何内容)即可。
public static void main(String[] args)
throws IOException
{
URL url = new URL("http://stackoverflow.com/");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String s = null;
while ((s = reader.readLine()) != null)
System.out.println(s);
}
您可以直接使用Java类:
URL url = new URL("http://www.example.com");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
...
,但推荐使用Apache HttpClient为HttpClient的将要处理很多事情,你必须做你自己的Java本机类它的更多。