本文实例讲述了JAVA获取任意http网页源代码。分享给大家供大家参考,具体如下:
1. 获取任意http网页的代码
2. 获取任意http网页去掉HTML标签的代码
Webpage类:
?
|
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
|
/**
* 网页操作相关类
*/
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author winddack
*
*/
public class Webpage {
private String pageUrl;//定义需要操作的网页地址
private String pageEncode="UTF8";//定义需要操作的网页的编码
public String getPageUrl() {
return pageUrl;
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
public String getPageEncode() {
return pageEncode;
}
public void setPageEncode(String pageEncode) {
this.pageEncode = pageEncode;
}
//定义取源码的方法
public String getPageSource()
{
StringBuffer sb = new StringBuffer();
try {
//构建一URL对象
URL url = new URL(pageUrl);
//使用openStream得到一输入流并由此构造一个BufferedReader对象
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), pageEncode));
String line;
//读取www资源
while ((line = in.readLine()) != null)
{
sb.append(line);
}
in.close();
}
catch (Exception ex)
{
System.err.println(ex);
}
return sb.toString();
}
//定义一个把HTML标签删除过的源码的方法
public String getPageSourceWithoutHtml()
{
final String regEx_script = "<script[^>]*?>[\\\\s\\\\S]*?<\\\\/script>"; // 定义script的正则表达式
final String regEx_style = "<style[^>]*?>[\\\\s\\\\S]*?<\\\\/style>"; // 定义style的正则表达式
final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
final String regEx_space = "\\\\s*|\\t|\\r|\\n";//定义空格回车换行符
String htmlStr = getPageSource();//获取未处理过的源码
Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
Matcher m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
Matcher m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
Matcher m_space = p_space.matcher(htmlStr);
htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
htmlStr = htmlStr.trim(); // 返回文本字符串
htmlStr = htmlStr.replaceAll(" ", "");
htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
return htmlStr;
}
}
|
调用:
?
|
1
2
3
4
|
Webpage page=new Webpage();
page.setPageUrl("http://www.baidu.com");
String code=page.getPageSourceWithoutHtml();
System.out.println(code);
|
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/dackwind/article/details/48732083
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 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-25 33
-
2025-05-29 94
-
2025-05-27 60
-
2025-05-27 54
-
2025-05-29 33
热门评论

