用java获取360doc页面上的url地址并在浏览器中自动打开
2017-03-10 13:58
549 查看
一、前言
最近在看一些前端的文章360doc,每次选中需要的url,都会弹出一个提示框,然后关闭后又自动转发到另一个页面,让人觉的很烦,于是便有了下面这个想法的产生。二、java实现获取指定页面url并在浏览器中自动打开
package com.linbilin.urltool.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class UrlUtils { public static void main(String[] args) throws Exception { try { String url = "http://www.360doc.com/content/15/0923/23/15118019_501136375.shtml"; Document doc = Jsoup.connect(url).get(); Element element = doc.getElementById("artContent"); String text = element.text(); String[] urlCommands = jointUrlCommands(getUtlInText(text)); for (String command : urlCommands) { Thread.sleep(500);// 间隔0.5秒打开一个页面,这么做的原因是浏览器打开的窗口多的话,很耗内存,所以你这边间隔的时间可以设置长一点 Runtime.getRuntime().exec(command);// 用默认浏览器打开链接 } } catch (IOException e) { e.printStackTrace(); } } /** * 把url组装成命令 */ public static String[] jointUrlCommands(String[] urls) { int len = urls.length; String command = "rundll32 url.dll,FileProtocolHandler "; for (int i = 0; i < len; i++) { urls[i] = command + urls[i]; } return urls; } /** * 获取文本中的url * * @param text * @return */ public static String[] getUtlInText(String text) { String regex = "(?i)https?://[\\S&&[^【]]+"; Matcher m = Pattern.compile(regex).matcher(text); List<String> urls = new ArrayList<String>(); while (m.find()) { urls.add(m.group()); } String[] arr = new String[urls.size()]; return urls.toArray(arr); } }
输出的URL:
http://www.xueui.cn/design/online-tools http://www.css88.com/doc/codeguide/ http://viewportsizes.com/?filter= http://top.css88.com/archives/660 http://pxtoem.com/ http://emmet.evget.com/ http://www.css88.com/tool/csstidy/ http://code.ciaoca.com/style/css-cheat-sheet/ http://oscarotero.com/jquery/ http://jsonlint.com/ http://www.jsoneditoronline.org/ http://www.atool.org/ http://my.oschina.net/maomi/blog/137807 http://www.css88.com/tool/jQuerySourceViewer/#v=1.7.2&fn=jQuery.find https://icomoon.io/app/#/select http://www.iconfont.cn/ http://fontawesome.io/icons/ http://www.w3cfuns.com/tools.php?mod=regex http://www.w3cfuns.com/tools.php http://meyerweb.com/eric/tools/css/diagnostics/demo-not.html http://cli.im/ http://code.ciaoca.com/style/cssfont2unicode/ http://www.atool.org/pngcompression.php http://nibbler.silktide.com/ https://lodash.com/docs http://ueditor.baidu.com/doc/#toolbar http://runjs.cn/code/vvd7bpzb http://www.bootcss.com/p/lesscss/ https://www.jstree.com/docs/json/ http://api.highcharts.com/highcharts http://requirejs.org/ https://cnodejs.org/getstart http://top.css88.com/archives/680 http://newhtml.net/category/v8%E4%B8%93%E9%A2%98/ http://www.infoq.com/cn/articles/subversion-front-end-ui-development-framework-react http://cubiq.org/iscroll-5 http://www.jeasyui.net/ https://github.com/justjavac/free-programming-books-zh_CN https://github.com/vhf/free-programming-books/blob/master/free-programming-books-zh.md https://github.com/papers-we-love/papers-we-love https://github.com/Yixiaohan/codeparkshare https://github.com/iamjoel/python-learn#%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%BA%90 https://github.com/hzlzh/Best-App https://github.com/nemoTyrant/manong https://github.com/nemoTyrant/manong https://github.com/youyudehexie/node123 https://github.com/youyudehexie/node123 https://github.com/justjavac/AngularJS-Learning-zh_CN https://github.com/enaqx/awesome-react https://github.com/AlloyTeam/Mars https://github.com/lvwzhen/iconpark https://github.com/codylindley/frontend-tools https://github.com/iamjoel/be-grace-front-end-developer https://github.com/ZhangBohan/http-api-design-ZH_CN https://github.com/JacksonTian/fks https://github.com/ZhangBohan/http-api-design-ZH_CN https://github.com/johnpolacek/superscrollorama https://github.com/impress/impress.js https://github.com/impress/impress.js http://dingxiangming.com
打开的页面:
三、提示
在浏览器中打开太多窗口的话,会很耗内存,所以尽量不要打开太多相关文章推荐
- 自动获取当前页面地址(url)的脚本
- C#获取默认浏览器的完整地址并在默认浏览器中打开对应的url地址
- php-curl获取接口图片地址,在php拼接img中无法访问,报403错误,在浏览器url中直接可以访问,html页面里img也可以访问
- yii2获取登录前的页面url地址--电脑和微信浏览器上的实现以及yii2相关源码的学习
- AS:Flash AS3中获取浏览器信息及URL相关参数(并非swf url地址)
- 获取浏览器的当前页面地址
- 在Wordpress的文章页面获取上一页及下一页的链接URL地址,实现通过键盘的前进后退键进行翻页
- Java学习笔记之网络编程基础-通过URL获取HTML页面
- asp.net局部页面打印,以及如何去掉打印时自动保留的URL地址(页眉页脚)
- java获取浏览器或服务器的全部url
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
- java 实现文件下载的时候,避免浏览器自动打开文件,而是出现另存为对话框
- java获取url内容及正则匹配链接图片地址
- Java 代码 调用浏览器 打开url (支持多个操作系统)
- window.open方法打开的页面,在服务器端怎么获取opener的url
- 页面自动刷//判断浏览器是否IE //注册ocx文件//打开远程连接对话框命令
- javascript获取本页面URL地址
- XP定时关机 && 浏览器新打开页面无法首次激活 && 关闭移动硬盘自动播放 && IE8从新选项卡打开链接 && XP虚拟机无法烧录软件
- java获取url地址的主域名正则表达式
- [InfoPath]如何在可在浏览器中打开的InfoPath表单中获取Url参数