Java利用Hutool工具类库抓取网址后随机生成姓名

第一次找的百家号网站读不出来 奇怪

感觉还是依葫芦画瓢  真自己写不太能从头到尾写出来

复制代码
 1 import cn.hutool.core.io.FileUtil;
 2 import cn.hutool.core.util.ReUtil;
 3 import cn.hutool.http.HttpUtil;
 4 
 5 import java.io.File;
 6 import java.util.*;
 7 
 8 public class HutoolTest1 {
 9     public static void main(String[] args) {
10         File StudentFile = new File("Z:\\AAAA\\Student.txt");
11         if (StudentFile.length() < 1000) {
12             ArrayList<String> StudentName = getName(50, 50);
13             FileUtil.writeLines(StudentName, StudentFile, "UTF-8");
14             System.out.println("已创建文件");
15         } else {
16             System.out.println("已存在文件 请进行核对");
17         }
18     }
19 
20     public static ArrayList<String> getName(int boyNum, int girlNum) {
21         String FamilyNameWeb = HttpUtil.get("https://zhuanlan.zhihu.com/p/25056561");
22         String BoyNameWeb = HttpUtil.get("https://zhuanlan.zhihu.com/p/429728068");
23         String GirlNameWeb = HttpUtil.get("https://zhuanlan.zhihu.com/p/408315781");
24         //使用正则获得所有标题
25         List<String> Familytitles = ReUtil.findAll("(. ){7}.", FamilyNameWeb, 0);
26         List<String> Boytitles = ReUtil.findAll("([0-9]、)(.{11})", BoyNameWeb, 2);
27         List<String> Girltitles = ReUtil.findAll("..、..、..、..、..", GirlNameWeb, 0);
28 
29 
30         //获得姓 男生名 女生名 三个List
31         ArrayList<String> FamilyName = new ArrayList<>();
32         for (String familytitle : Familytitles) {
33             String[] temp = familytitle.split(" ");
34             for (String string : temp) {
35                 FamilyName.add(string);
36             }
37         }
38         ArrayList<String> BoyName = new ArrayList<>();
39         for (String boytitle : Boytitles) {
40             String[] temp = boytitle.split("、");
41             for (String string : temp) {
42                 BoyName.add(string);
43             }
44         }
45         ArrayList<String> GirlName = new ArrayList<>();
46         for (String girltitle : Girltitles) {
47             String[] temp = girltitle.split("、");
48             for (String string : temp) {
49                 GirlName.add(string);
50             }
51         }
52 
53         //通过哈希表构造男生和女生名字List  为了防止重复
54         HashSet<String> boyhs = new HashSet<>();
55         HashSet<String> girlhs = new HashSet<>();
56         Random r = new Random();
57         while (true) {
58             if (boyhs.size() == boyNum) {
59                 break;
60             }
61             Collections.shuffle(FamilyName);
62             Collections.shuffle(BoyName);
63             boyhs.add(FamilyName.get(0) + BoyName.get(0));
64         }
65         while (true) {
66             if (girlhs.size() == girlNum) {
67                 break;
68             }
69             Collections.shuffle(FamilyName);
70             Collections.shuffle(GirlName);
71             girlhs.add(FamilyName.get(0) + GirlName.get(0));
72         }
73         ////构造学生List
74         ArrayList<String> StudentName = new ArrayList<>();
75         for (String boyh : boyhs) {
76             int age = r.nextInt(11) + 18;
77             StudentName.add(boyh + "-" + age);
78         }
79         for (String girlh : girlhs) {
80             int age = r.nextInt(11) + 18;
81             StudentName.add(girlh + "-" + age);
82         }
83         Collections.shuffle(StudentName);
84         return StudentName;
85 
86     }
87 }
View Code
复制代码

 

posted @   风来三醒  阅读(153)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示