Java 正则表达式爬取内容

Java 正则表达式爬取内容

	public static void main(String[] args) {
		
		String str1 = "电话028-29586748,或者邮箱12873213@qq.com"+
				"电话028-22136748,或者邮箱333873213@qq.com"+
				"电话18161666666,或者邮箱128722113@163.com";
				
		//从上面内容爬取电话号码和邮箱
		//1.定义爬取规则
		String regex = "(\\w{1,}@\\w{2,10}(\\.\\w{2,10}){1,2})|(1[3-9]\\d{9})|(0[2-5]\\d{1,9}-?\\d{8})|(400-?\\d{3,8}-?\\d{3,8})";
		//2.把正则表达式进行编译成为一个匹配规则对象
		Pattern compile = Pattern.compile(regex);
		//3.通过匹配规则对象得到一个匹配数据内容的匹配器对象
		Matcher matcher = compile.matcher(str1);
		
		//4.通过匹配器去内容中爬取信息
		while(matcher.find()){
			System.out.println(matcher.group());
		}
		
	}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,需要使用Java中的URL类来获取网页内容,然后使用正则表达式来匹配敏感词。以下是一个简单的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WebCrawler { public static void main(String[] args) { String url = "https://www.example.com"; String[] sensitiveWords = {"sensitive1", "sensitive2", "sensitive3"}; try { URL websiteUrl = new URL(url); BufferedReader in = new BufferedReader(new InputStreamReader(websiteUrl.openStream())); StringBuilder sb = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } in.close(); String webpageContent = sb.toString(); for (String word : sensitiveWords) { Pattern pattern = Pattern.compile(word); Matcher matcher = pattern.matcher(webpageContent); if (matcher.find()) { System.out.println("Sensitive word found: " + word); } } } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例代码中,我们首先定义了要爬取的网页URL和敏感词数组。然后,我们使用URL类获取网页内容,并将其存储在字符串变量中。接下来,我们使用正则表达式以遍历敏感词数组,对每个敏感词都进行一次匹配。如果找到了敏感词,就输出相应的信息。 当然,这只是一个简单的示例代码。在实际应用中,可能需要更加复杂的正则表达式或者其他的技术来对网页内容进行分析和过滤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值