一、代码
爬虫的实质就是打开网页源代码进行匹配查找,然后获取查找到的结果。
打开网页:
?
|
1
|
URL url = new URL(http://www.cnblogs.com/Renyi-Fan/p/6896901.html);
|
读取网页内容:
?
|
1
|
BufferedReader bufr = new BufferedReader(new InputStreamReader(url.openStream()));
|
正则表达式进行匹配:
?
|
1
|
tring mail_regex = "\\\\w+@\\\\w+(\\\\.\\\\w+)+";
|
储存结果:
?
|
1
|
List<String> list = new ArrayList<String>();
|
/*
* 获取
* 将正则规则进行对象的封装。
* Pattern p = Pattern.compile("a*b");
* //通过正则对象的matcher方法字符串相关联。获取要对字符串操作的匹配器对象Matcher .
* Matcher m = p.matcher("aaaaab");
* //通过Matcher匹配器对象的方法对字符串进行操作。
* boolean b = m.matches();
*/
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Spider {
public static void main(String[] args) throws IOException {
// List<String> list = getMails();
// for(String mail : list){
// System.out.println(mail);
// }
List<String> list = getMailsByWeb();
for(String mail : list){
System.out.println(mail);
}
}
public static List<String> getMailsByWeb() throws IOException{
//1,读取源文件。
//URL url = new URL("http://192.168.1.100:8080/myweb/mail.html");
//URL url = new URL("http://localhost:8080/SecondWeb/index.jsp");
URL url = new URL("http://www.cnblogs.com/Renyi-Fan/p/6896901.html");
BufferedReader bufr = new BufferedReader(new InputStreamReader(url.openStream()));
//2,对读取的数据进行规则的匹配。从中获取符合规则的数据.
String mail_regex = "\\\\w+@\\\\w+(\\\\.\\\\w+)+";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile(mail_regex);
String line = null;
while((line=bufr.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
//3,将符合规则的数据存储到集合中。
list.add(m.group());
}
}
return list;
}
public static List<String> getMails() throws IOException{
//1,读取源文件。
BufferedReader bufr = new BufferedReader(new FileReader("c:\\\\mail.html"));
//2,对读取的数据进行规则的匹配。从中获取符合规则的数据.
String mail_regex = "\\\\w+@\\\\w+(\\\\.\\\\w+)+";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile(mail_regex);
String line = null;
while((line=bufr.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
//3,将符合规则的数据存储到集合中。
list.add(m.group());
}
}
return list;
}
}
|
二、运行结果
?
|
1
2
|
abc1@sina.com.cn
1@1.1
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://www.cnblogs.com/Renyi-Fan/p/6897023.html
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 12
-
2025-05-25 75
-
2025-05-27 32
-
2025-06-05 88
-
2025-05-29 103
热门评论

