java获取静态页面内容
package collection_map;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
public static String delHTMLTag(String htmlStr) {
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(""); // 过滤空格回车标签
return htmlStr.trim(); // 返回文本字符串
}
public static String getTextFromHtml(String htmlStr){
htmlStr = delHTMLTag(htmlStr);
htmlStr = htmlStr.replaceAll(" ", "");
htmlStr = htmlStr.substring(0, htmlStr.indexOf("。")+1);
return htmlStr;
}
public static void main(String[] args) {
// String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
// String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
// String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
// String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
URL url;
try {
// get URL content
url = new URL("http://ssl.gongyi.qq.com/m/weixin/detail_yqj_commentList.html?pg=2&did=1215372601201609087100023344&oid=oproJj0dWhq4R_jCp3iYZgb3cbPY");//目标URL
URLConnection conn = url.openConnection();//打开URL
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf-8"));
String inputLine;
//save to this filename
String fileName = "test.txt";//建立URL
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
// bw.write(inputLine.replaceAll("[^(0-9\\u4e00-\\u9fa5)]", ""));
String str = getTextFromHtml(inputLine);
bw.write(str);
}
bw.close();
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java获取静态页面内容的更多相关文章
- JAVA获取txt文件内容
JAVA 读取txt文件内容 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可. public class txttest { /** * 读 ...
- Python手动构造Cookie模拟登录后获取网站页面内容
最近有个好友让我帮忙爬取个小说,这个小说是前三十章直接可读,后面章节需要充值VIP可见.所以就需要利用VIP账户登录后,构造Cookie,再用Python的获取每章节的url,得到内容后再使用 PyQ ...
- java 获取微信 页面授权 获取用户openid
先调用微信的地址 跳转https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4b4009c4fce00e0c&redirect ...
- java 获取网页指定内容
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...
- java 获取网页指定内容-2(实践+修改)
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; ...
- java 获取URL链接 内容
public static String getHtmlConentByUrl(String ssourl) { try { URL url = new URL( ...
- delphi WebBrowser获取iframe页面内容及操作
uses MSHTML, ActiveX; function GetFrame(FrameNo:Integer):IWebbrowser2;var OleContainer:IOleContainer ...
- Java解析html页面,获取想要的元素
背景:通过接口访问数据,获取的内容是个标准的html格式,使用jsoup的方式获取页面元素值 先推荐比较好的博客:http://www.open-open.com/jsoup/. 单个案例比较不错 h ...
- 获取windows身份认证网站页面内容
有些网站必须登录才能获取到页面内容. 代码如下,可获取数据. var url = "https://yunda-api-test.appspot.com/int/parcel?wait=tr ...
随机推荐
- @Configuration 和 @Bean
1. @Bean: 1.1 定义 从定义可以看出,@Bean只能用于注解方法和注解的定义. @Target({ElementType.METHOD, ElementType.ANNOTATION_TY ...
- jQuery 3.1.1 官方下载地址
https://code.jquery.com/jquery-3.1.1.jshttps://code.jquery.com/jquery-3.1.1.min.js 打包下载: http:// ...
- 使用Servlet实现图片下载
package chensi.com; import java.io.FileInputStream; import java.io.IOException; import java.net.URLE ...
- linux卸载openJDK并安装sun jdk
linux下卸载openJDK并安装java 1.查找现在有的jdk rpm -qa | grep java 2.删除jdk rpm -e --nodeps java----- 3.安装jdk 下载j ...
- ini_set 设置php配置项 在windows和linux下的不同
在win下,当你要include多个路径的话,你要用“:”隔开,但在linux下就使用":"隔开的.. if (substr(php_uname(), 0, 7) == " ...
- 第一章 JacksonUtil 序列化与反序列化属性总结
1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...
- React Developer Tools 安装小提示
1,在google市场里边,安装React Developer Tools之后,发现是开启的,但是按下F12后,并没有发现react选项 2,后来通过查资料,发现必须是运行react项目的时候,才出现 ...
- StringGrid 实例2:1、获取 StringGrid 的行数、列数; 2、给单元赋值.
实例2: 本例功能: 1.获取 StringGrid 的行数.列数; 2.给单元赋值. 运行效果图:
- ubuntu工具积累
1.sudo apt-get install terminator一款可以切分终端窗口的工具 a.在系统>键盘>快捷键修该ctrl+alt+t快捷应用为terminator,其他的快捷键同 ...
- .net 泛型运用
DAL层 private DbContext MyContext; public BaseRepository(DbContext context) { MyContext = context; } ...