利用Java實現(xiàn)解析網(wǎng)頁中的內(nèi)容
一、題目描述
題目實現(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)文章
深入學(xué)習(xí)Java編程中的字符串的進(jìn)階使用
這篇文章主要介紹了Java編程中的字符串的高級運用,包括StringBuffer類和StringTokenizer類以及常量池的介紹,需要的朋友可以參考下2016-01-01如何解決UnsupportedOperationException異常問題
這篇文章主要介紹了如何解決UnsupportedOperationException異常問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05IDEA創(chuàng)建SpringBoot項目整合mybatis時mysql-connector-java報錯異常的詳細(xì)分析
最近工作中發(fā)現(xiàn)了個錯誤,分享給同樣遇到這個問題的朋友,這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建SpringBoot項目整合mybatis時mysql-connector-j報錯異常的詳細(xì)分析,需要的朋友可以參考下2023-02-02ElasticSearch學(xué)習(xí)之Es索引Api操作
這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之Es索引Api操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01