Java获取任意http网页源代码的方法分享
来源:互联网 发布:网络上轮子党什么意思 编辑:程序博客网 时间:2024/06/05 02:16
这篇文章主要介绍了Java获取任意http网页源代码的方法,可实现获取网页代码以及去除HTML标签的代码功能,涉及Java正则操作相关实现技巧,需要的朋友可以参考下
本文实例讲述了JAVA获取任意http网页源代码。分享给大家供大家参考,具体如下:
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);
转转:http://www.php.cn/java-article-379597.html
阅读全文
0 0
- Java获取任意http网页源代码的方法分享
- java 获取网页源代码
- Java获取网页源代码
- Java获取网页源代码
- java 获取网页源代码
- java获取特定网页的源代码
- Java程序获取网页源代码
- java获取响应网页源代码
- VC++通过HTTP方式获取网页源代码
- c#获取网页源代码的几种方法
- c#获取网页源代码的几种方法
- 3种获取网页源代码的方法(WEBBROWSE.INET.WSOCK)
- C#获取网页源代码的5种方法
- C#获取网页源代码的5种方法
- asp获取网页源代码并处理乱码的方法
- 获取某网页的源代码
- 利用Java的net包获取网页源代码
- Java获取网页信息的方法
- COMMAND 命令模式 --对象行为型模式
- ORA-12154: TNS: 无法解析指定的连接标识符
- 晒桌面 | 非主流五线产品汪的简单原木风工作台
- 中国特色的税收制度和不动产投资
- 集合枚举
- Java获取任意http网页源代码的方法分享
- 第四周项目三(2)两个链表的连接
- [分享]程序大战-学习资料
- 积分商城如何梳理思路和进行设计
- Qt Creator下载和安装(详细教程)
- 如何把手机直播系统开发做到更好?
- POJ
- 使用Java Io文件的读写
- DNS隧道工具iodine