你如何以编程方式下载 Java 网页

编程入门 行业动态 更新时间:2024-10-28 08:17:33
本文介绍了你如何以编程方式下载 Java 网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够获取网页的 html 并将其保存到 String,以便我可以对其进行一些处理.另外,我如何处理各种类型的压缩.

I would like to be able to fetch a web page's html and save it to a String, so I can do some processing on it. Also, how could I handle various types of compression.

我将如何使用 Java 做到这一点?

How would I go about doing that using Java?

推荐答案

这里是一些使用 Java 的测试代码 URL 类.不过,我建议在处理异常或将它们传递到调用堆栈方面做得比我在这里做得更好.

Here's some tested code using Java's URL class. I'd recommend do a better job than I do here of handling the exceptions or passing them up the call stack, though.

public static void main(String[] args) { URL url; InputStream is = null; BufferedReader br; String line; try { url = new URL("stackoverflow/"); is = url.openStream(); // throws an IOException br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { System.out.println(line); } } catch (MalformedURLException mue) { mue.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException ioe) { // nothing to see here } } }

更多推荐

你如何以编程方式下载 Java 网页

本文发布于:2023-07-27 05:20:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1220577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   网页   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!