抓取网页姓和名,生成假数据

2023-12-15 18:04:19

最终生成格式为:张三-男-23,分辨代表姓名、性别和年龄。

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 爬取网页内容,生成随机男孩女孩姓名
 */
public class WebNameCrawler {
    /**
     * main方法
     * 姓氏 https://zhuanlan.zhihu.com/p/671961620
     * 女孩姓名 https://www.shopwang.net/38945345.html
     * 男孩姓名 https://www.shopwang.net/93977424.html
     *
     * @param args
     */
    public static void main(String[] args) throws IOException {
        String familyNamesURL = "https://zhuanlan.zhihu.com/p/671961620";     // 姓
        String girlNamesURL = "https://www.shopwang.net/38945345.html";       // 女孩名字
        String boyNamesURL = "https://www.shopwang.net/93977424.html";        // 男孩名字

        // 1.获取姓氏
        String familyNamesHTML = getHTMLText(familyNamesURL);
        ArrayList<String> familyNames = getWords(familyNamesHTML, "([\\u4E00-\\u9FA5]|[\\u4E00-\\u9FA5]{2})(、|。)", 1);
        //System.out.println(familyNames.size());
        System.out.println(familyNames);

        // 2.获取女孩名
        String girlHTML = getHTMLText(girlNamesURL);
        ArrayList<String> girlList = getWords(girlHTML, "(([\\u4E00-\\u9FA5]{2}、){4}[\\u4E00-\\u9FA5]{2})", 0);
        // System.out.println(girlList);
        // 模板元素:诗琳、若婉、勤凡、瑾汐、琬兮
        ArrayList<String> girlNames = new ArrayList<>();
        for (String s : girlList) {
            String[] names = s.split("、");
            for (String name : names) {
                girlNames.add(name);
            }
        }
        System.out.println(girlNames);

        // 3.获取男孩名
        String boyHTML = getHTMLText(boyNamesURL);
        ArrayList<String> boyList = getWords(boyHTML, "(([\\u4E00-\\u9FA5]{2}、){4}[\\u4E00-\\u9FA5]{2})", 0);
        // System.out.println(girlList);
        // 模板元素:诗琳、若婉、勤凡、瑾汐、琬兮
        ArrayList<String> boyNames = new ArrayList<>();
        for (String s : boyList) {
            String[] names = s.split("、");
            for (String name : names) {
                boyNames.add(name);
            }
        }
        System.out.println(boyNames);

        // 4.生成dummpy姓名
        ArrayList<String> dummyNames = getDummyNames(familyNames, boyNames, girlNames, 70, 50);
        System.out.println(dummyNames);

        // 5.写出
        FileWriter fw = new FileWriter(new File("files/dummyNames"+System.currentTimeMillis()+".txt"));
        BufferedWriter bw = new BufferedWriter(fw);
        for (String dummyName : dummyNames) {
            bw.write(dummyName);
            bw.newLine();
        }
        bw.flush();
        bw.close();
    }

    /**
     * 生成假名字
     * @param familyNameList 姓列表
     * @param boyNameList 男孩名列表
     * @param girlNameList 女孩名列表
     * @param boyCount 要生成几个男孩名
     * @param girlCount 要生成几个女孩名
     * @return 生成好的姓名列表
     */
    public static ArrayList<String> getDummyNames(ArrayList<String> familyNameList,
                                                  ArrayList<String> boyNameList, ArrayList<String> girlNameList,
                                                  int boyCount, int girlCount){
        ArrayList<String> dummyNames = new ArrayList<>();
        // 1.生成男孩姓名
        while (dummyNames.size() < boyCount){
            String name = familyNameList.get(new Random().nextInt(familyNameList.size()))
                    + boyNameList.get(new Random().nextInt(boyNameList.size()));
            if (!dummyNames.contains(name)){
                dummyNames.add(name + "-男-" + (new Random().nextInt(5) + 20));
            }
        }

        // 2.生成女孩姓名
        while (dummyNames.size() < (boyCount + girlCount)){
            String name = familyNameList.get(new Random().nextInt(familyNameList.size()))
                    + girlNameList.get(new Random().nextInt(girlNameList.size()));
            if (!dummyNames.contains(name)){
                dummyNames.add(name + "-女-" + (new Random().nextInt(15) + 20));
            }
        }

        return dummyNames;
    }

    /**
     * 用正则表达式摘取想要的数据并返回
     * @param src 源文本
     * @param regex 正则表达式
     * @param position 利用Matcher.group(int)获取字符的位置
     * @return 正则匹配的文本列表
     */
    public static ArrayList<String> getWords(String src, String regex, int position){
        // 存放返回字符的列表
        ArrayList<String> list = new ArrayList<>();
        // 正则匹配
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(src);
        // 循环读取
        while (m.find()){
            String bit = m.group(position);
            if (!list.contains(bit)){
                list.add(bit);
            }
        }
        // 返回列表
        return list;
    }

    /**
     * getHTMLText(URL url)方法的重载
     * @param urlStr
     * @return
     * @throws IOException
     */
    public static String getHTMLText(String urlStr) throws IOException {
        return getHTMLText(new URL(urlStr));
    }

    /**
     * 获取制定URL的HTML文本
     *
     * @param url HTML网页的URL
     * @return 网页的HTML文本
     */
    public static String getHTMLText(URL url) throws IOException {
        // 1.打开链接
        URLConnection conn = url.openConnection();
        // 2.获取输入流Reader读取数据
        InputStreamReader isr = new InputStreamReader(conn.getInputStream());
        // 3.缓冲区
        char[] buffer = new char[1024 * 128];
        // 4.读取长度
        int len;
        // 5.StringBuilder类存放html文本
        StringBuilder sb = new StringBuilder();
        // 6.循环读取
        while ((len = isr.read(buffer)) != -1) {
            sb.append(buffer, 0, len);
        }
        // 7.返回文本
        return sb.toString();
    }

}

文章来源:https://blog.csdn.net/weixin_50083448/article/details/135021064
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。