如何在 Java 中获取 HTML

发布于 2024-07-04 15:25:16 字数 50 浏览 6 评论 0原文

在不使用任何外部库的情况下,将网站的 HTML 内容提取到字符串中的最简单方法是什么?

Without the use of any external library, what is the simplest way to fetch a website's HTML content into a String?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挖鼻大婶 2024-07-11 15:25:16

我目前正在使用这个:

String content = null;
URLConnection connection = null;
try {
  connection =  new URL("http://www.google.com").openConnection();
  Scanner scanner = new Scanner(connection.getInputStream());
  scanner.useDelimiter("\\Z");
  content = scanner.next();
  scanner.close();
}catch ( Exception ex ) {
    ex.printStackTrace();
}
System.out.println(content);

但不确定是否有更好的方法。

I'm currently using this:

String content = null;
URLConnection connection = null;
try {
  connection =  new URL("http://www.google.com").openConnection();
  Scanner scanner = new Scanner(connection.getInputStream());
  scanner.useDelimiter("\\Z");
  content = scanner.next();
  scanner.close();
}catch ( Exception ex ) {
    ex.printStackTrace();
}
System.out.println(content);

But not sure if there's a better way.

友谊不毕业 2024-07-11 15:25:16

它不是库,而是一个名为curl的工具,通常安装在大多数服务器中,或者您可以通过以下方式轻松安装在ubuntu中然后

sudo apt install curl

获取任何html页面并将其存储到本地文件(如示例)

curl https://www.facebook.com/ > fb.html

您将获得主页html。您可以运行它也在您的浏览器中。

Its not library but a tool named curl generally installed in most of the servers or you can easily install in ubuntu by

sudo apt install curl

Then fetch any html page and store it to your local file like an example

curl https://www.facebook.com/ > fb.html

You will get the home page html.You can run it in your browser as well.

转角预定愛 2024-07-11 15:25:16

这对我来说效果很好:

URL url = new URL(theURL);
InputStream is = url.openStream();
int ptr = 0;
StringBuffer buffer = new StringBuffer();
while ((ptr = is.read()) != -1) {
    buffer.append((char)ptr);
}

不确定提供的其他解决方案是否更有效。

This has worked well for me:

URL url = new URL(theURL);
InputStream is = url.openStream();
int ptr = 0;
StringBuffer buffer = new StringBuffer();
while ((ptr = is.read()) != -1) {
    buffer.append((char)ptr);
}

Not sure at to whether the other solution(s) provided are any more efficient or not.

随风而去 2024-07-11 15:25:16

我刚刚在您的其他帖子中留下了这篇文章,尽管你上面的方法也可能有效。 我认为其中任何一个都不比另一个更容易。 只需在代码顶部使用 import org.apache.commons.HttpClient 即可访问 Apache 包。

编辑:忘记链接了;)

I just left this post in your other thread, though what you have above might work as well. I don't think either would be any easier than the other. The Apache packages can be accessed by just using import org.apache.commons.HttpClient at the top of your code.

Edit: Forgot the link ;)

风情万种。 2024-07-11 15:25:16
 try {
        URL u = new URL("https"+':'+'/'+'/'+"www.Samsung.com"+'/'+"in"+'/');
        URLConnection urlconnect = u.openConnection();
        InputStream stream = urlconnect.getInputStream();
        int i;
        while ((i = stream.read()) != -1) {
            System.out.print((char)i);
        }
    }
    catch (Exception e) {
        System.out.println(e);
    }
 try {
        URL u = new URL("https"+':'+'/'+'/'+"www.Samsung.com"+'/'+"in"+'/');
        URLConnection urlconnect = u.openConnection();
        InputStream stream = urlconnect.getInputStream();
        int i;
        while ((i = stream.read()) != -1) {
            System.out.print((char)i);
        }
    }
    catch (Exception e) {
        System.out.println(e);
    }
无声无音无过去 2024-07-11 15:25:16

虽然不是普通的 Java,但我将提供一个更简单的解决方案。 使用 Groovy ;-)

String siteContent = new URL("http://www.google.com").text

Whilst not vanilla-Java, I'll offer up a simpler solution. Use Groovy ;-)

String siteContent = new URL("http://www.google.com").text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文