欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Java實現(xiàn)解析網(wǎng)頁中的內(nèi)容

 更新時間:2022年10月11日 11:43:15   作者:小虛竹and掘金  
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言做一個解析指定網(wǎng)址的網(wǎng)頁內(nèi)容小應(yīng)用,文中的實現(xiàn)步驟講解詳細(xì),感興趣的可以嘗試下

一、題目描述

題目實現(xiàn):做一個解析指定網(wǎng)址的網(wǎng)頁內(nèi)容小應(yīng)用。

二、解題思路

創(chuàng)建一個類:InternetContentFrame,繼承JFrame窗體類。

定義一個getURLCollection()方法:用于解析網(wǎng)頁內(nèi)容

使用URLConnection類的getInputStream()方法 獲取網(wǎng)頁資源的輸入流對象。

三、代碼詳解

InternetContentFrame

package com.xiaoxuzhu;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
 * Description: 
 *
 * @author xiaoxuzhu
 * @version 1.0
 *
 * <pre>
 * 修改記錄:
 * 修改后版本	        修改人		修改日期			修改內(nèi)容
 * 2022/5/23.1	    xiaoxuzhu		2022/5/23		    Create
 * </pre>
 * @date 2022/5/23
 */
public class InternetContentFrame extends JFrame {

    private JTextArea ta_content;
    private JTextField tf_address;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InternetContentFrame frame = new InternetContentFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame
     */
    public InternetContentFrame() {
        super();
        setTitle("解析網(wǎng)頁中的內(nèi)容");
        setBounds(100, 100, 484, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.NORTH);

        final JLabel label = new JLabel();
        label.setText("輸入網(wǎng)址:");
        panel.add(label);

        tf_address = new JTextField();
        tf_address.setPreferredSize(new Dimension(260,25));
        panel.add(tf_address);

        final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                String address = tf_address.getText().trim();// 獲得輸入的網(wǎng)址
                Collection urlCollection = getURLCollection(address);// 調(diào)用方法,獲得網(wǎng)頁內(nèi)容的集合對象
                Iterator it = urlCollection.iterator();  // 獲得集合的迭代器對象
                while(it.hasNext()){
                    ta_content.append((String)it.next()+"\n");       // 在文本域中顯示解析的內(nèi)容
                }
            }
        });
        button.setText("解析網(wǎng)頁");
        panel.add(button);

        final JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        ta_content = new JTextArea();
        ta_content.setFont(new Font("", Font.BOLD, 14));
        scrollPane.setViewportView(ta_content);
        //
    }
    public Collection<String> getURLCollection(String urlString){
        URL url = null;                             // 聲明URL
        URLConnection conn = null;                  // 聲明URLConnection
        Collection<String> urlCollection = new ArrayList<String>(); // 創(chuàng)建集合對象
        try{
            url = new URL(urlString);               // 創(chuàng)建URL對象
            conn = url.openConnection();            // 獲得連接對象
            conn.connect();                         // 打開到url引用資源的通信鏈接
            InputStream is = conn.getInputStream(); // 獲取流對象
            InputStreamReader in = new InputStreamReader(is,"UTF-8"); // 轉(zhuǎn)換為字符流
            BufferedReader br = new BufferedReader(in); // 創(chuàng)建緩沖流對象
            String nextLine = br.readLine();            // 讀取信息,解析網(wǎng)頁
            while (nextLine !=null){
                urlCollection.add(nextLine);   // 解析網(wǎng)頁的全部內(nèi)容,添加到集合中
                nextLine = br.readLine();      // 讀取信息,解析網(wǎng)頁
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return urlCollection;
    }

}

解析結(jié)果:

到此這篇關(guān)于利用Java實現(xiàn)解析網(wǎng)頁中的內(nèi)容的文章就介紹到這了,更多相關(guān)Java解析網(wǎng)頁內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論