Java获取任意http网页源代码的方法
本文实例讲述了JAVA获取任意http网页源代码。分享给大家供大家参考,具体如下:

10余年的包河网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整包河建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“包河网站设计”,“包河网站推广”以来,每个客户项目都认真落实执行。
JAVA获取任意http网页源代码可实现如下功能:
1. 获取任意http网页的代码
2. 获取任意http网页去掉HTML标签的代码
Webpage类:
/**
* 网页操作相关类
*/
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 = "
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-26 02:37:52 HTTP/1.1 GET : /article/ppicdd.html
- 运行时间 : 0.0565s ( Load:0.0009s Init:0.0207s Exec:0.0301s Template:0.0048s )
- 吞吐率 : 17.70req/s
- 内存开销 : 470.96 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=q91hrn6181qkleoiicmnnr8ho5
- /home/wwwroot/bluegullmediac/wwwroot/index.php ( 1.09 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Common/Conf/config.php ( 2.16 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Conf/config.php ( 0.31 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 18.20 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000007s ]
- [ app_init ] --END-- [ RunTime:0.000035s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000049s ]
- [ app_begin ] --END-- [ RunTime:0.000065s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000074s ]
- [ template_filter ] --END-- [ RunTime:0.000110s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.004118s ]
- [ view_parse ] --END-- [ RunTime:0.004154s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000054s ]
- [ view_filter ] --END-- [ RunTime:0.000069s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/bluegullmediac/wwwroot/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 122 行.

0.0565s
