JavaRegex讀取網頁內容程式碼

來源:互聯網
上載者:User

 

 代碼如下 複製代碼

package com.xiaofeng.picup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** *//**
*
* @抓取雅虎知識堂的文章標題及內容(測試) 手動輸入網址抓取,可進一步自動抓取整個知識堂的全部內容
*
*/
public class WebContent ...{
    /** *//**
     * 讀取一個網頁全部內容
     */
    public String getOneHtml(String htmlurl) throws IOException...{
        URL url;
        String temp;
        StringBuffer sb = new StringBuffer();
        try ...{
            url = new URL(htmlurl);
            BufferedReader in = new BufferedReader(new InputStreamReader(url
                    .openStream(), "utf-8"));// 讀取網頁全部內容
            while ((temp = in.readLine()) != null) ...{
                sb.append(temp);
            }
            in.close();
        }catch(MalformedURLException me)...{
            System.out.println("你輸入的URL格式有問題!請仔細輸入");
            me.getMessage();
            throw me;
        }catch (IOException e) ...{
            e.printStackTrace();
            throw e;
        }
        return sb.toString();
    }

    /** *//**
     *
     * @param s
     * @return 獲得網頁標題
     */
    public String getTitle(String s) ...{
        String regex;
        String title = "";
        List<String> list = new ArrayList<String>();
        regex = "<title>.*?</title>";
        Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
        Matcher ma = pa.matcher(s);
        while (ma.find()) ...{
            list.add(ma.group());
        }
        for (int i = 0; i < list.size(); i++) ...{
            title = title + list.get(i);
        }
        return outTag(title);
    }

    /** *//**
     *
     * @param s
     * @return 獲得連結
     */
    public List<String> getLink(String s) ...{
        String regex;
        List<String> list = new ArrayList<String>();
        regex = "<a[^>]*href=("([^"]*)"|'([^']*)'|([^s>]*))[^>]*>(.*?)</a>";
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
        Matcher ma = pa.matcher(s);
        while (ma.find()) ...{
            list.add(ma.group());
        }
        return list;
    }

    /** *//**
     *
     * @param s
     * @return 獲得指令碼代碼
     */
    public List<String> getScript(String s) ...{
        String regex;
        List<String> list = new ArrayList<String>();
        regex = "<script.*?</script>";
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
        Matcher ma = pa.matcher(s);
        while (ma.find()) ...{
            list.add(ma.group());
        }
        return list;
    }

    /** *//**
     *
     * @param s
     * @return 獲得CSS
     */
    public List<String> getCSS(String s) ...{
        String regex;
        List<String> list = new ArrayList<String>();
        regex = "<style.*?</style>";
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
        Matcher ma = pa.matcher(s);
        while (ma.find()) ...{
            list.add(ma.group());
        }
        return list;
    }

    /** *//**
     *
     * @param s
     * @return 去掉標記
     */
    public String outTag(String s) ...{
        return s.replaceAll("<.*?>", "");
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.