您的位置:首页 > 编程语言 > Java开发

用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


打开的页面:



三、提示

在浏览器中打开太多窗口的话,会很耗内存,所以尽量不要打开太多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐