java刷访客
开始之前请先准备一个解析json数据的jar包 这里推荐fastjson请自行到https://github.com/alibaba/fastjson/releases去下载并添加到项目不想下载的话可以用第二个版本版本一package com.liziguo.csdn;/*** @Author: Liziguo* @Date: 2021/3/11 12:22*/public class Artic
·
开始之前请先准备一个解析json数据的jar包 这里推荐fastjson
请自行到https://github.com/alibaba/fastjson/releases去下载并添加到项目
不想下载的话可以用第二个版本
版本一
package com.liziguo.csdn;
/**
* @Author: Liziguo
* @Date: 2021/3/11 12:22
*/
public class Article {
public final String title;
public final String url;
public Article(String title, String url) {
this.title = title;
this.url = url;
}
}
package com.liziguo.csdn;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Liziguo
* @Date: 2021/3/11 10:38
*/
public class Main {
public static String id = "u010756046";//主页后面那段就是你的id https://blog.csdn.net/u010756046
public static List<Article> list;
private static int oldAccessCount;
public static String open(String url) {
HttpURLConnection conn = null;
BufferedReader in = null;
BufferedWriter out = null;
try {
URL u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET"); // 设置请求方式
if (conn.getResponseCode() == 200) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
StringBuilder str = new StringBuilder();
while ((line = in.readLine()) != null) {
str.append(line);
}
return str.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null)
conn.disconnect();
}
return null;
}
public static String formatString(double data) {
DecimalFormat df = new DecimalFormat("#,###.00");
return df.format(data);
}
public static String formatString(int data) {
DecimalFormat df = new DecimalFormat("#,###");
return df.format(data);
}
public static List<Article> getArticle() {
List<Article> list = new ArrayList<>();
//如果你的文章大于100条 请修改size=100
String text = open("https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=100&businessType=blog&orderby=&noMore=false&username=" + id);
JSONArray jsonArray = JSON.parseObject(text).getJSONObject("data").getJSONArray("list");
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Article article = new Article(jsonObject.getString("title"), jsonObject.getString("url"));
list.add(article);
}
return list;
}
public static int getAccessCount() {
int start, end;
String text = open("https://blog.csdn.net/" + id);
start = text.indexOf("user-profile-statistics-num");
start = text.indexOf(">", start) + 1;
end = text.indexOf("<", start);
return Integer.parseInt(text.substring(start, end).replace(",", ""));
}
public static void main(String[] args) {
System.out.println("正在访问主页...");
oldAccessCount = getAccessCount();
System.out.println("正在获取博文...");
list = getArticle();
for (Article article : list) {
System.out.println(article.url + " " + article.title);
}
System.out.println("获取博文成功 " + list.size() + "篇(如果您的博文超过100篇请自行修改代码)");
while (true) {
for (Article article : list) {
System.out.println("正在打开:" + article.url + " " + article.title);
open(article.url);
}
System.out.println("初始访问量:" + formatString(oldAccessCount));
int accessCount = getAccessCount();
System.out.println("当前访问量:" + formatString(accessCount));
System.out.println("访问量增加:" + formatString(accessCount - oldAccessCount));
System.out.println("点赞加关注小果爱你哟");
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
不使用jar版
package com.liziguo.csdn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Liziguo
* @Date: 2021/3/11 10:38
*/
public class Main {
public static String id = "u010756046";//主页后面那段就是你的id https://blog.csdn.net/u010756046
public static List<Article> list;
private static int oldAccessCount;
public static String open(String url) {
HttpURLConnection conn = null;
BufferedReader in = null;
BufferedWriter out = null;
try {
URL u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET"); // 设置请求方式
if (conn.getResponseCode() == 200) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
StringBuilder str = new StringBuilder();
while ((line = in.readLine()) != null) {
str.append(line);
}
return str.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null)
conn.disconnect();
}
return null;
}
public static String formatString(double data) {
DecimalFormat df = new DecimalFormat("#,###.00");
return df.format(data);
}
public static String formatString(int data) {
DecimalFormat df = new DecimalFormat("#,###");
return df.format(data);
}
public static List<Article> getArticle() {
int cursor = 0;
List<Article> list = new ArrayList<>();
//如果你的文章大于100条 请修改size=100
String text = open("https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=100&businessType=blog&orderby=&noMore=false&username=" + id);
final String str1 = "\"title\"";
final String str2 = "\"url\"";
while (true) {
cursor = text.indexOf(str1, cursor);
if (cursor < 0) break;
String title = text.substring(cursor + 9, text.indexOf("\"", cursor + 9));
cursor = text.indexOf(str2, cursor);
String url = text.substring(cursor + 7, text.indexOf("\"", cursor + 7));
System.out.println(url + " " + title);
Article article = new Article(title, url);
list.add(article);
}
return list;
}
public static int getAccessCount() {
int start, end;
String text = open("https://blog.csdn.net/" + id);
start = text.indexOf("user-profile-statistics-num");
start = text.indexOf(">", start) + 1;
end = text.indexOf("<", start);
return Integer.parseInt(text.substring(start, end).replace(",", ""));
}
public static void main(String[] args) {
System.out.println("正在访问主页...");
oldAccessCount = getAccessCount();
System.out.println("正在获取博文...");
list = getArticle();
for (Article article : list) {
System.out.println(article.url + " " + article.title);
}
System.out.println("获取博文成功 " + list.size() + "篇(如果您的博文超过100篇请自行修改代码)");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true) {
for (Article article : list) {
System.out.println("正在打开:" + article.url + " " + article.title);
open(article.url);
}
System.out.println("博文数量:" + list.size());
System.out.println("初始访问量:" + formatString(oldAccessCount));
int accessCount = getAccessCount();
System.out.println("当前访问量:" + formatString(accessCount));
System.out.println("访问量增加:" + formatString(accessCount - oldAccessCount));
System.out.println("点赞加关注小果爱你哟");
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)