html使用第三方标签,不使用第三方框架获取html页面某个标签的某个属性值

public static String httpSendGet(String url, String param,String charsetName) {

String result = "";

BufferedReader in = null;

try {

String urlNameString = url + "?" + param;

URL realUrl = new URL(urlNameString);

// 打开和URL之间的连接

URLConnection connection = realUrl.openConnection();

// 设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 建立实际的连接

connection.connect();

// 定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(new InputStreamReader(

connection.getInputStream(), charsetName));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送GET请求出现异常!" + e);

e.printStackTrace();

}

// 使用finally块来关闭输入流

finally {

try {

if (in != null) {

in.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

return result;

}

工具方法match:

public static List match(String source, String element, String attr) {

List result = new ArrayList();

String reg = "]*?\\s" + attr + "=[‘\"]?(.*?)[‘\"]?(\\s.*?)?>";

Matcher m = Pattern.compile(reg).matcher(source);

while (m.find()) {

String r = m.group(1);

result.add(r);

}

return result;

}

调用demo:

public static void main(String[] args) {

String url = "https://www.dy2018.com/i/99671.html";

String params = "";

String html = httpSendGet(url,params,"gb2312");

List links = match(html,"a","href");

System.out.println(links);

}

这里需要说明一下,httpSendGet的charsetName参数需要注意,不然你获取的html文本会是乱码。

最后展示一下结果(当然结果还不纯粹,需要过滤):

[/, /2/, /0/, /3/, /1/, /4/, /8/, /5/, /7/, /14/, /15/, /html/tv/hytv/index.html, /html/tv/oumeitv/index.html, /html/tv/rihantv/index.html, /html/zongyi2013/index.html, /html/dongman/index.html, /support/GuestBook.php, #, index.html, /, /html/tv/, /html/tv/hytv/, javascript:window.external.addFavorite(‘http://www.dy2018.com/‘,‘dy2018.com-电影天堂‘)"class="style11, /webPlay/play-id-99671-collection-37.html, /webPlay/play-id-99671-collection-36.html, /webPlay/play-id-99671-collection-35.html, /webPlay/play-id-99671-collection-34.html, /webPlay/play-id-99671-collection-33.html, /webPlay/play-id-99671-collection-32.html, /webPlay/play-id-99671-collection-31.html, /webPlay/play-id-99671-collection-30.html, /webPlay/play-id-99671-collection-29.html, /webPlay/play-id-99671-collection-28.html, /webPlay/play-id-99671-collection-27.html, /webPlay/play-id-99671-collection-26.html, /webPlay/play-id-99671-collection-25.html, /webPlay/play-id-99671-collection-24.html, /webPlay/play-id-99671-collection-23.html, /webPlay/play-id-99671-collection-22.html, /webPlay/play-id-99671-collection-21.html, /webPlay/play-id-99671-collection-20.html, /webPlay/play-id-99671-collection-19.html, /webPlay/play-id-99671-collection-18.html, /webPlay/play-id-99671-collection-17.html, /webPlay/play-id-99671-collection-16.html, /webPlay/play-id-99671-collection-15.html, /webPlay/play-id-99671-collection-14.html, /webPlay/play-id-99671-collection-13.html, /webPlay/play-id-99671-collection-12.html, /webPlay/play-id-99671-collection-11.html, /webPlay/play-id-99671-collection-10.html, /webPlay/play-id-99671-collection-9.html, /webPlay/play-id-99671-collection-8.html, /webPlay/play-id-99671-collection-7.html, /webPlay/play-id-99671-collection-6.html, /webPlay/play-id-99671-collection-5.html, /webPlay/play-id-99671-collection-4.html, /webPlay/play-id-99671-collection-3.html, /webPlay/play-id-99671-collection-2.html, /webPlay/play-id-99671-collection-1.html, /webPlay/play-id-99671-collection-0.html, ftp://g:[email protected]:2166/一千零一夜35.mp4, ftp://g:[email protected]:2166/一千零一夜34.mp4, ftp://g:[email protected]:2166/一千零一夜33.mp4, ftp://g:[email protected]:2166/一千零一夜32.mp4, ftp://g:[email protected]:2166/一千零一夜31.mp4, ftp://g:[email protected]:2166/一千零一夜30.mp4, ftp://g:[email protected]:2166/一千零一夜29.mp4, ftp://g:[email protected]:2166/一千零一夜28.mp4, ftp://g:[email protected]:2166/一千零一夜27.mp4, ftp://g:[email protected]:2166/一千零一夜26.mp4, ftp://g:[email protected]:2166/一千零一夜25.mp4, ftp://g:[email protected]:2166/一千零一夜24.mp4, ftp://g:[email protected]:2166/一千零一夜23.mp4, ftp://g:[email protected]:2166/一千零一夜22.mp4, ftp://g:[email protected]:2166/一千零一夜21.mp4, ftp://g:[email protected]:2166/一千零一夜20.mp4, ftp://g:[email protected]:2166/一千零一夜19.mp4, ftp://g:[email protected]:2166/一千零一夜18.mp4, ftp://g:[email protected]:2166/一千零一夜17.mp4, ftp://g:[email protected]:2166/一千零一夜16.mp4, ftp://g:[email protected]:2166/一千零一夜15.mp4, ftp://g:[email protected]:2166/一千零一夜14.mp4, ftp://g:[email protected]:2166/一千零一夜13.mp4, ftp://g:[email protected]:2166/一千零一夜12.mp4, ftp://g:[email protected]:2166/一千零一夜11.mp4, ftp://g:[email protected]:2166/一千零一夜10.mp4, ftp://g:[email protected]:2166/一千零一夜09.mp4, ftp://g:[email protected]:2166/一千零一夜08.mp4, ftp://g:[email protected]:2166/一千零一夜07.mp4, ftp://g:[email protected]:2166/一千零一夜06.mp4, ftp://g:[email protected]:2166/一千零一夜05.mp4, ftp://g:[email protected]:2166/一千零一夜04.mp4, ftp://g:[email protected]:2166/一千零一夜03.mp4, ftp://g:[email protected]:2166/一千零一夜02.mp4, ftp://g:[email protected]:2166/一千零一夜01.mp4, /i/99743.html, /i/99734.html, /i/99733.html, /i/99725.html, /i/99720.html, /i/99719.html, /i/99716.html, /i/99708.html, /i/99704.html, /i/99695.html, /i/97129.html, /i/97575.html, /i/97041.html, /i/92091.html, /i/97637.html, /i/92020.html, /i/95187.html, /i/92000.html, /i/98343.html, /i/97363.html]

不想自己写正则表达式,可以使用第三方爬虫框架,这方面网上找很多的,我就不写了。

原文:http://blog.51cto.com/yuqian2203/2145120

前端html5框架ZUI发布1.2正式版本了! 此版本增加了很多新特性,同时修复问题,文档也得到大幅完善。 欢迎大家使用并反馈问题, 更新明细: 1、新增日历视图组件,方便实现日程的展示 2、新增数据表格视图组件,更轻松实现复杂数据的展示,移除table.data.js 3、重新实现的模态框触发器,模态框触发器同时支持加载远程内容和实时内容,实时根据自身内容调整位置和尺寸,重新设计了调用接口,使用更加方便,消除了模态框触发器于一般模态框发生冲突的隐患 4、package.json增加组件及其依赖关系的配置,重新实现了Grunt任务,编译单独组件更加方便,并支持任意组件集合的打包编译,grunt任务将自动管理组件之间的依赖关系 5、大幅更新文档部分章节内容,并对文档整理样式进行了调整,在文档中可以查看每个组件属性,并给出第三方组件版本及用户支持信息,文档支持IE8,优化文档在小屏幕上的表现 6、重新实现了漂浮消息组件,调用更方便,更好的动画效果 7、新增新的第三方组件chartjs,能够绘制简单图表 8、增加browser.js,为IE系列的浏览器增加版本提示辅助类 9、新增 'store.js',实现本地存储通用接口,并增加单独页面范围的独立存储机制,beta版本中的页面标识有时不正确的bug已得到修复 10、新增 'jquery.extensions.js',增加一些实用的jQuery扩展方法,便于进行其他组件的开发,移除原'unities.js' 11、增加array.js,提供一些操作数组的实用方法 12、调整了dist目录结构,第三方组件和适合单独调用的组件将直接包含在lib目录中 13、Chosen选项及分组支持title属性,Chosen弹出列表支持自定义宽度,调整多选Chosen中选项的样式 14、修复Chosen中特定情况下placeholder无法显示的问题 15、日期时间选择器将能够自动从html标签获取页面语言设置并应用语言设置 16、调整左侧固定导航样式 17、在date.js中增加一些实用方法来帮助进行日期计算 18、模态框支持额外的两种默认尺寸,修复某些时候模态框弹出时滚动条闪动的问题 19、修复代码段第一行出现错位的样式问题 20、一些组件的额外组成部分其对应的文件被重新命名 21、修复特定情况下kindeditor图标无法显示的问题 22、优化区块面板视图动作按钮事件监听机制 23、修复color.js中增加命名颜色支持 26、弹出框增加新的选项能够制定JS生成DOM的id属性,便于自定义样式 27、大幅优化代码,修复一些在Javascript代码中的错误,完善关键代码注释,增强部分代码文件与requierejs的兼容性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值