java获取网页上邮箱地址存放到.txt文件

本文介绍了一种通过解析网页源代码来查找并提取邮件地址的方法,包括如何使用Java编程实现,涉及URL请求、正则表达式匹配等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.test;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

public class GetEmailDemo {
/**
* 测试方法;
*
* @param args
*/
public static void main(String[] args) {
GetEmailDemo emailDemo = new GetEmailDemo();
@SuppressWarnings("unused")
String error = emailDemo
.getWebContent("http://www.tradesns.com/forum_messages.php?mid=24877&ccid=5906");
System.out.println("邮件地址查找完成...");
}
/**
* 获得网页中的源代码; 逐行解析;
*
* @param path
* @return
*/
private String getWebContent(String path) {
BufferedReader bufferedReader = null;
StringBuffer sb = new StringBuffer();
if (path != null && !"".equals(path)) {
try {
URL url = new URL(path);
bufferedReader = new BufferedReader(new InputStreamReader(
url.openStream()));
String line = null;
System.out.println("开始分析邮件地址...");
String filePath = "e:/email/";
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
String str2 = "";
while ((line = bufferedReader.readLine()) != null) {
String str = parse(line);
if (!str.equals("")) {
str2 += str;
}
}
if (!str2.equals("")) {
String[] arrstr = str2.split(" ");
FileWriter fw = new FileWriter(filePath + "email.txt");
PrintWriter pw = new PrintWriter(fw);
//去重复
Set<String> set = new TreeSet<String>();
for (String i : arrstr) {
set.add(i);
}
String[] dest = new String[set.size()];
Iterator<String> iter = set.iterator();
for (int i = 0; i < dest.length && iter.hasNext(); i++) {
dest[i] = iter.next();
}
//输出
for (int i = 0; i < dest.length; i++) {
pw.write(dest[i] + "\n");
}
pw.close();
fw.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
sb.append(e.toString());
} catch (IOException e) {
e.printStackTrace();
sb.append(e.toString());
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
sb.append(e.toString());
}
}
}
return sb.toString();
}

/**
* 分析数据;
*
* @param line
*/
private String parse(String line) {
String str = "";
if (line != null && line.length() > 0) {
// 邮箱正则表达式;
String regexExpression = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+";
Pattern pattern = Pattern.compile(regexExpression);
Matcher matcher = pattern.matcher(line);
try {
while (matcher.find()) {
String str2 = matcher.group();
if (!str2.equals("")) {
str += str2 + " ";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return str;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值