package com.wbg.my.service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* @author Jack Chen
* */
public class BlogUtil { /**
* URL_PAGE:cnblogs url
* URL_PAGE_DETAIL:详情页url
* PAGE_COUNT:页数
* urlLists:所有详情页url Set集合(防止重复)
* p:匹配模式
* */
public final static String URL_PAGE = "https://www.cnblogs.com/weibanggang/default.html?page=";
public final static String URL_PAGE_DETAIL = "https://www.cnblogs.com/weibanggang/p/([0-9]+.html)";
public final static int PAGE_COUNT = 20;
public static Set<String> urlLists = new TreeSet<String>();
public final static Pattern p = Pattern.compile(URL_PAGE_DETAIL);
//文件路径
public static String file="d:index.html";
static String [] arr=null;
static int sun=0;
public static void main(String[] args) throws Exception {
for(int i = 1;i<=PAGE_COUNT;i++) {
getUrls(i);
}
System.out.println("开始获取内容!");
arr=new String[urlLists.size()];
for(Iterator<String> i = urlLists.iterator();i.hasNext();) {
createFile(i.next());
sun++;
}
System.out.println("获取内容完毕!");
System.out.println("开始写入文件!");
StringBuffer stringBuffer=new StringBuffer(kais());
for (int i = 0; i < arr.length; i++) {
stringBuffer.append(arr[i]);
}
stringBuffer.append(jiehun());
System.out.println("写入文件完毕!");
System.out.println("开始导出文件!");
createFile(file,stringBuffer);
System.out.println("导出文件完毕!");
System.out.println("输出文件地址为:"+file);
}
/*
* 将结果写入文件
*/
private static void createFile(String file, StringBuffer buffer) {
try {
File newFile = new File(file);
if (newFile.exists())// 存在,则删除
if (!newFile.delete())// 删除成功则创建
{
System.err.println("删除文件" + newFile + "失败");
}
if (newFile.createNewFile()) {// 创建成功,则写入文件内容
PrintWriter p = new PrintWriter(new FileOutputStream(newFile
.getAbsolutePath()));
p.write(buffer.toString());
p.close();
} else {
System.err.println("创建文件:" + newFile + "失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//开始头部
public static String kais(){
return "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta charset=\"utf-8\">\n" +
" <title>weibanggang.github.io</title>\n" +
" <meta name=\"renderer\" content=\"webkit\">\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">\n" +
" <style>\n" +
" html,body{width:100%;height: 100%}\n" +
" table{width: 1150px;height:500px;margin: auto}\n" +
" table,td,th{border: 1px solid #e6e6e6;border-collapse:collapse; }\n" +
" body{-moz-background-size:100% 100%; background-size:100% 100%;background-image:url(\"link.jpg\");background-repeat: no-repeat} body{-moz-background-size:100% 100%; background-size:100% 100%;background-image:url(\"link.jpg\");background-repeat: no-repeat}\n" +
" * { margin: 0; padding: 0; }\n" +
" table { border-collapse: collapse; text-align: center; }\n" +
" /*关键设置 tbody出现滚动条*/\n" +
" table tbody {\n" +
" display: block;\n" +
" height: 500px;\n" +
" overflow-y: scroll;overflow-x:hidden;\n" +
" }\n" +
" table thead, tbody tr { display: table;width: 100%; table-layout: fixed; }\n" +
" table thead th { height: 40px }\n" +
" table tbody td {height: 30px }\n" +
" </style>\n" +
"</head>\n" +
"\n" +
"<body>\n" +
"<marquee><h1 style=\"color:white;\">本网页仅作为参考博客、github等地址</h1></marquee>\n" +
"<table width=\"80%\" border=\"1\">\n" +
" <thead>\n" +
" <tr>\n" +
" <th style=\"width:230px\">序号</th>\n" +
" <th style=\"width:231px\">标题链接</th>\n" +
" <th style=\"width:231px\">时间</th>\n" +
" <th style=\"width:231px\">来源</th>\n" +
" <th style=\"width:249px\">备注</th>\n" +
" </tr>\n" +
" </thead>\n" +
" <tbody>\n" +
"\n" +
" </tbody>\n" +
"</table>\n" +
"</body>\n" +
"<script src=\"js/jquery.js\"></script>\n" +
"<script>\n" +
" var sum=[";
}
//结尾
public static String jiehun(){
return " ];\n" +
" \n" +
" for(var i=0;i<sum.length;i++){\n" +
" var tr=$(\"<tr/>\");\n" +
" //序号\n" +
" $(\"<td/>\").html(i+1).appendTo(tr);\n" +
" //标题链接\n" +
" var a=\"<a href='\"+sum[i][0]+\"' target='_blank'>\"+sum[i][1]+\"</a>\"\n" +
" $(\"<td/>\").html(a).appendTo(tr);\n" +
" //时间\n" +
" $(\"<td/>\").html(sum[i][2]).appendTo(tr);\n" +
" //来源\n" +
" $(\"<td/>\").html(sum[i][3]).appendTo(tr);\n" +
" //备注\n" +
" $(\"<td/>\").html(sum[i][4]).appendTo(tr);\n" +
" $(\"table tbody\").append(tr);\n" +
" }\n" +
"</script>\n" +
"</html>";
}
static String fh="";
/**
* @param url
* 获取所有内容
* @throws
*/
private static void createFile(String url) throws Exception {
Matcher m = p.matcher(url);
m.find();
String fileName = m.group(1);
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String str;
StringBuffer s=new StringBuffer();
while((str = br.readLine()) != null){
s.append(str);
}
String href="https://www.cnblogs.com/weibanggang/p/"+fileName;
String title=getTitle(s);
String data=getDate(s);
arr[sun]=fh+"[\""+href+"\",\""+title+"\",\""+data+"\",\"博客\",\"正常\"]";
fh=",";
br.close();
conn.disconnect();
}
//获取时间
public static String getDate(StringBuffer sb){
int first=sb.indexOf("<span id=\"post-date\">")+"<span id=\"post-date\">".length();
String aa=sb.substring(first);
int last=aa.indexOf("</span>");
String sa=aa.substring(0,last);
return sa;
}
//获取标题
public static String getTitle(StringBuffer sb){
int first=sb.indexOf("<title>");
int last=sb.indexOf("</title>");
String sa=sb.substring(first+7,last);
int errorindex=sa.lastIndexOf("- 韦邦杠 - 博客园");
return sa.substring(0,errorindex);
}
/**
* @param idx
* 获取页数
* @throws
*/
private static void getUrls(int idx) throws Exception{
URL u = new URL(URL_PAGE+""+idx);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String str;
while((str = br.readLine()) != null){
if(null != str && str.contains("https://www.cnblogs.com/weibanggang/p/")) {
Matcher m = p.matcher(str);
if(m.find()) {
urlLists.add(m.group());
}
}
}
br.close();
conn.disconnect();
} }

java提取(获取)博客信息(内容)的更多相关文章

  1. 如何提取CSDN博客正文内容

    document.getElementById("article_content").outerHTML; 在任意的一片博文运行以上代码都可以获得正文内容,但是对于代码.字体都没有 ...

  2. 02_使用WebMagic爬虫获取CSDN推荐专家的个人博客信息

    本来是想抓取博客园的博客推荐的页面的,但由于一些博客进去的页面格式都不太相同,一时不想花时间去寻找规律,发现CSDN上面的格式较为单一,就决定以CSDN推荐专家的个人博客信息作为爬虫抓取的目标. [首 ...

  3. windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息

    scrapy作为流行的python爬虫框架,简单易用,这里简单介绍如何使用该爬虫框架爬取个人博客信息.关于python的安装和scrapy的安装配置请读者自行查阅相关资料,或者也可以关注我后续的内容. ...

  4. Java课程设计博客(个人)

    Java课程设计博客(个人) 1. 团队课程设计博客链接 http://www.cnblogs.com/wkfg/p/7063081.html 2. 个人负责模块或任务说明 负责模块/任务:编写doG ...

  5. 获取博客积分排名,存入数据库,读取数据进行绘图(python,selenium,matplotlib)

    该脚本的目的:获取博客的排名和积分,将抓取时间,排名,积分存入数据库,然后把最近的积分和排名信息进行绘图,查看积分或者排名的变化情况. 整个脚本的流程:是利用python3来编写,利用selnium获 ...

  6. Java课程设计——博客作业教学数据分析系统(201521123082 黄华林)

    Java课程设计--博客作业教学数据分析系统(201521123082 黄华林) 一.团队课程设计博客链接 博客作业教学数据分析系统(From:网络五条狗) 二.个人负责模块或任务说明 1.网络爬虫 ...

  7. Java课程设计博客(团队)

    Java课程设计博客(团队) 1. 团队/项目名称 使用JAVA实现简易HTTP服务器 2. 团队成员 组长:林一心 组员:张杭镖 3. 项目git地址 https://github.com/oran ...

  8. Java实现个人博客网站

    说明:该项目是实验楼用户"LOU3165780622"发布在实验楼上的项目教程:[Java实现个人博客],未经允许,禁止转载: 该项目利用 SSM 框架和 Mysql 以及一些简单 ...

  9. 推荐一款自研的Java版开源博客系统OneBlog

    OneBlog 一款超好用的Java版开源博客      Introduction 简介 OneBlog 一个简洁美观.功能强大并且自适应的Java博客.使用springboot开发,前端使用Boot ...

随机推荐

  1. HTML5之WebSocket && https://zhuanlan.zhihu.com/p/23467317

    在认识websocket之前,我们必须了解的是websocket有什么用? 他能解决我们遇到的什么问题? 如果没用,那么我们就么有使用它的必要的. websocket就是建立起全双工协议的,提高了效率 ...

  2. 【程序员技术练级】学习一门脚本语言 python(三)跟数据库打交道

    接着上一篇,该篇讲述使用python对数据库进行基本的CRUD操作,这边以sqlite3为例子,进行说明.sqlite3 是一个非常轻型的数据库,安装和使用它是非常简单的,这边就不进行讲述了. 在py ...

  3. 微信小程序支付c#后台实现

    今天为大家带来比较简单的支付后台处理 首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOp ...

  4. json对象字符串互转

    json对象字符串互转 1.Node.js中 JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json ...

  5. mvc中在Action里调用另一个Action

    今天做东西时发现一个新东西.即在一个Action调用另一Action.前提是同一个控制器.(没在一个控制里的没试过) 调用方法: public   ActionResult Test1(){ //to ...

  6. C语言的前世今生

    1.计算机语言的发展 机器语言:其实就是二进制0和1,最小为00000000,最大为11111111,8位比特为1个字节(byte),1k=1024byte,1m=1024k,1g=1024m[第一代 ...

  7. 数组和矩阵(3)——Next Greater Element I

    https://leetcode.com/problems/next-greater-element-i/#/description You are given two arrays (without ...

  8. Csharp:字符串操作

    public class StringControl { /// <summary> /// 客户端浏览器 /// http://en.wikipedia.org/wiki/Web_bro ...

  9. android 5.0 下载编译

    CM的CM-12.0版本(对应Android5.0.2): $ repo init -u https://github.com/CyanogenMod/android.git -b cm-12.0 注 ...

  10. ArcGIS Enterprise 10.5.1 静默安装部署记录(Centos 7.2 minimal)- 1、安装前准备

    安装前准备 上传文件到服务器,x-ftp   xshell登陆Centos 检查机器名 修改机器名为:portal.cloud.local   方法一:零时设置,重启后失效,该方法不可取     方法 ...