Java如何讀取和下載網頁?

2019-10-16 22:28:05

在Java程式設計中,如何讀取和下載網頁?

以下範例顯示如何使用net.URL類的URL()建構函式來讀取和下載網頁。

package com.yiibai;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;

public class DownloadingWebpage {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://www.tw511.com");
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        BufferedWriter writer = new BufferedWriter(new FileWriter("save2yiibai-index.html"));
        String line;

        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            writer.write(line);
            writer.newLine();
        }
        reader.close();
        writer.close();
    }
}

上述程式碼範例將產生以下結果(輸出易百教學的首頁頁面原始碼,並儲存到save2yiibai-index.html檔案中) -

<!--
輸出易百教學的首頁頁面原始碼
-->
<!DOCTYPE HTML>
<html>
<head><!--

-->
<!DOCTYPE HTML>
<html>
<head>
... ... 省略

範例-2

Java讀取和下載網頁的另一個範例:

package com.yiibai;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadingWebpage2 {
    public static void main(String[] args) {
        URL url;
        InputStream is = null;
        BufferedReader br;
        String line;
        try {
            url = new URL("/25/11214.html");
            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) {
            }
        }
    }
}

上述程式碼範例將產生以下結果(輸出頁面原始碼) -

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
...... 省略