Java 網絡 URL 從網頁上獲取數據

簡單瞭解URL功能
實現從網頁上獲取數據

package com.mashensoft.net;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

/**
 * Url
 * 
 * @author PeicongHe
 *
 */

public class UrlDemo {
    /**
     * 原來我們用手工通過瀏覽器訪問網頁,現在通過JAVA代碼訪問網頁 URL
     * 統一資源定位符(URL,英語UniformResourceLocator的縮寫)也被稱爲網頁地址,是因特網上標準的資源的地址。
     * 獲取網易雲音樂的信息
     */
    public static void test1() {
        try {
            URL url = new URL("http://music.163.com/");
            System.out.println(url.getHost());// 獲取網絡地址
            System.out.println(url.getFile());// 獲取文件
            System.out.println(url.getPort());// 獲取端口號
            System.out.println(url.getProtocol());// 獲取網絡協議
            System.out.println(url.getDefaultPort());// 獲取端口
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 從網頁上獲取內容
     */
    public static void test2() {
        try {
            URL url = new URL("http://music.163.com/");
            URLConnection conn = url.openConnection();// 打開鏈接
            InputStream is = conn.getInputStream();// 獲取數據流
            // URLConnection is =url.openStream();//縮寫
            Scanner sc = new Scanner(is, "GBK");// 掃描數據流並存放在sc裏,編碼方式 GBK
            while (sc.hasNextLine()) {// 判斷是否存在下一行
                System.out.println(sc.nextLine());// 輸出當前行內容
            }
            sc.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 由於訪問網絡需要連網,當不聯網的時候,我就沒辦法使用數據 所以,我們要把從網頁獲取的內容存到本地來 步驟: 1、從網上獲取數據 2、將數據存入文本
     */
    public static void test3() {
        StringBuffer sb = new StringBuffer();
        // Constructs a string buffer with no characters in it and an initial
        // capacity of 16 characters.

        try {
            URL url = new URL("http://music.163.com/");
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            Scanner sc = new Scanner(is, "UTF-8");
            // String content ="";
            // StringBuffer sb = new StringBuffer();
            while (sc.hasNextLine()) {
                sb.append(sc.nextLine()).append("\r\n");// 把當前行內容存放在StringBuffer
                                                        // sb裏 ,append()附加)
                // content+=sc.nextLine();
                // System.out.println(content+=sc.nextLine());
            }
            sc.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 把數據存放在文件中
        try {
            //// Creates a new PrintWriter, without automatic line flushing,
            //// with the specified file name.
            PrintWriter pw = new PrintWriter("content.txt");
            pw.println(sb.toString());
            pw.flush();
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("執行成功");
    }

    /**
     * 從網頁上獲取數據
     * 
     * @param myUrl
     * @return
     */
    public static String getContentFromUrl(String myUrl, String charset) {
        StringBuffer sb = new StringBuffer();
        URL url;
        try {
            url = new URL(myUrl);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            Scanner sc = new Scanner(is, charset);
            while (sc.hasNextLine()) {
                sb.append(sc.nextLine()).append("\r\n");
            }
            sc.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("執行成功");
        return sb.toString();
    }

    public static boolean writeContentToFile(String content, String fileName) {
        boolean sign = false;
        // 把數據存入在文件夾中
        PrintWriter pw;
        try {
            pw = new PrintWriter(fileName);
            pw.println(content);
            pw.flush();
            pw.close();
            sign = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            sign = false;
        }

        return sign;

    }

    /**
     * 輸出文件
     * 
     * @param args
     */
    public static String test3x() {
        StringBuffer sbx = new StringBuffer();
        try {
            URL urlx = new URL("https://www.baidu.com");
            InputStream uc = urlx.openStream();
            Scanner sc = new Scanner(uc, "UTF-8");
            while (sc.hasNextLine()) {
                sbx.append(sc.nextLine()).append("\r\n");
            }
            sc.close();
            uc.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("執行成功");
        return sbx.toString();
    }

    public static boolean test3xx(String content) {
        boolean sign = false;
        try {
            PrintWriter pw = new PrintWriter("a.html");
            pw.println(content);
            pw.flush();
            pw.close();
            sign = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            sign = false;
        }
        return sign;
    }

    public static void main(String[] args) {
        test1();
        // getContentFromUrl("https://www.baidu.com","UTF-8");
        // String content = getContentFromUrl("https://www.baidu.com","UTF-8");
        // boolean sign = writeContentToFile(content,"b.html");
        // String content = test3x();
        // boolean sign = test3xx(content);
        // System.out.println(sign);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章