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 ...
随机推荐
- android中5大布局
Android布局也可以用HTML5做,但是用户体验性差 Android布局里XML的属性配置 1. 五种Layout中Item的基础属性: layout_width & layout_hei ...
- 比较两个Long对象值
比较两个Long对象的值是否相等,不可以使用双等号进行比较,(long int Integer可以用双等号进行比较)可以采用如下方式: 1.使用equals方法进行比较 Long a=new Long ...
- 快速排序(java实现)
快速排序 算法思想:基于分治的思想,是冒泡排序的改进型.首先在数组中选择一个基准点(该基准点的选取可能影响快速排序的效率,后面讲解选取的方法),然后分别从数组的两端扫描数组,设两个指示标志(lo指向起 ...
- Sql Server 查询第30条数据到第40条记录数
1.select top 10 * from (select top 40 * from tablename order by id desc);
- [php-src]理解Php内核中的函数与INI
内容均以php-5.6.14为例. 一. 函数结构 内核中定义一个php函数使用 PHP_FUNCTION 宏 包装,扩展也不例外,该宏在 ./main/php.h:343 有着一系列类似以 PHP ...
- 巧用Javascript中的slice()
slice()是Javascript中Array的一个方法,定义是这样的. arrayObject.slice(start,end)作用是从原数组中从start到end位置截取出一个新的数组,返回值是 ...
- Points on cycle
Description There is a cycle with its center on the origin. Now give you a point on the cycle, you a ...
- hibernate报错及解决方式
1 java.sql.BatchUpdateException: ORA-01438: 值大于为此列指定的允许精度 原因:pl/sql number(n),插入的位数大于n
- 非官方windows下Cpython二进制扩展包下载地址
Unofficial Windows Binaries for Python Extension Packages url:http://www.lfd.uci.edu/~gohlke/pythonl ...
- 【洛谷P2296】寻找道路
反正图两边bfs #include<iostream> #include<cstdio> #include<queue> using namespace std; ...