上班小妙招,看看小说,学学代码

简单的从网上获取小说内容:

package com.company.web;

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

public class HttpRequest {
    //设置字符集
    static String charsetName = "gbk";

    HttpRequest(String htmlUrl) {
        URL url = null;
        URLConnection urlNet = null;
        InputStream in = null;
        BufferedReader bReader = null;
        HashMap map = new HashMap();
        try {
            url = new URL(htmlUrl);
            urlNet = url.openConnection();
            in = urlNet.getInputStream();
            bReader = new BufferedReader(new InputStreamReader(in, charsetName));
            String s = null;
            String html = "";
            while ((s = bReader.readLine()) != null) {
                html += s;
            }
            String title = getTitle(html);
            String pLabel = getPLabel(html);
            System.out.println(title);
            System.out.println(pLabel);
            in.close();
        } catch (IOException e) {
            System.out.println("无法打开网页!!");
            e.printStackTrace();
        }
    }


    /**
     * 获取title标签里面的内容
     *
     * @param html
     * @return
     */
    public String getTitle(String html) {
        String title = "";
        String regex = "<title>.*?</title>";
        List<String> list = new ArrayList<String>();
        Pattern pa = Pattern.compile(regex);
        Matcher ma = pa.matcher(html);
        while (ma.find())//寻找符合el的字串
        {
            list.add(ma.group());//将符合el的字串加入到list中
        }
        for (int i = 0; i < list.size(); i++) {
            title += list.get(i);
        }
        return outTag(title);
    }

    /**
     * 获取P标签里面的内容
     *
     * @param html
     * @return
     */
    public String getPLabel(String html) {
        String title = "";
        String regex = "<p.*?>(.*?)</p>";
        List<String> list = new ArrayList<String>();
        Pattern pa = Pattern.compile(regex);
        Matcher ma = pa.matcher(html);
        while (ma.find())//寻找符合el的字串
        {
            list.add(ma.group());//将符合el的字串加入到list中
        }
        for (int i = 0; i < list.size(); i++) {
            title += "\n" + list.get(i);
        }
        return outTag(title);
    }

    /**
     * 过滤前后标签
     *
     * @param str
     * @return
     */
    public String outTag(String str) {
        return str.replaceAll("<.*?>", "");
    }

    public static void main(String[] args) {
        //网站地址
        String htmlUrl = "https://www.x23qb.com/book/1081/228497.html";
        new HttpRequest(htmlUrl);
    }
}

效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值