使用random方法,和shuffle,随机的(伪随机),随机的假象。仅供学习,谢谢配合。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
import java.text.*;
import java.io.*;
import java.net.*;
*
* --------------------自动程序--------------------
*
* 将要测试的id填写入16行的变量userId中,点击运行
* 本程序将访问该博主【用户ID】名下所有,仅限学习测试
*
* 仅供学习测试使用~
*
public class ShuaJiZi {
static String userId = "Veer_c";
public static void main(String urlstr[]) throws IOException, InterruptedException {
Random r = new Random(6);//用于产生随机数的类。和种子相关(种子其实可以把它看成一个产生随机数的定义的规则)。Random()为没有指定种子
while(true){
try {
ShuaJiZi.fangfa();
//Sop("汇总:运行完毕,成功总增加:" + shiji++*urlss.size()+"哇...我太牛了");
} catch (Exception e) {
e.printStackTrace();
}
int suiji = r.nextInt(10);
Thread.currentThread().sleep(suiji*1000+136963);//毫秒*/ 延迟一会儿,小睡
}
}
public static void fangfa()throws IOException, InterruptedException{
Set<String> urls = new HashSet<String>();
// ----------------------------------------------遍历每一页 ----------------------------------------------
final String homeUrl = "https://blog.csdn.net/" + userId + "/article/list/";// 后面加pageNum即可
int totalPage = 0;
InputStream is;
String pageStr;
StringBuilder curUrl = null;
for (int i = 1; i < 100; i++) {
Thread.sleep(1000);
Sop("finding page " + i);
curUrl = new StringBuilder(homeUrl);
curUrl.append(i);
Sop(curUrl);
is = doGet(curUrl.toString());
pageStr = inputStreamToString(is, "UTF-8");// 一整页的html源码
List<String> list = getMatherSubstrs(pageStr, "(?<=href=\")https://blog.csdn.net/" + userId + "/article/details/[0-9]{8,9}(?=\")");
//Collections.shuffle(list);
urls.addAll(list);
if (pageStr.lastIndexOf("空空如也") != -1) {
Sop("No This Page!");
break;
} else {
Sop("Success~");
}
totalPage = i;
}
Sop("总页数为: " + totalPage);
// ---------------------------------------------------打印每个链接---------------------------------------------------
Sop("打印每个链接");
List<String> urlss =new ArrayList<String>(urls);
Collections.shuffle(urlss); //滴答洗牌
//urls.shuffle(string);
//Collections.shuffle(list);
//urls.shuffle();
for (String s:urlss) {
Sop(s);
}
Sop("打印每个链接完毕");
// ---------------------------------------------------访问每个---------------------------------------------------
int i=0;
Random r = new Random(3);
for (String s:urlss) {
doGet(s);
int suiji3 = r.nextInt(6);
Thread.currentThread().sleep(suiji3*1000);//毫秒*/
Sop("成功访问第" + (++i) + "个链接,共" + urlss.size() + "个:" + s);
}
// ---------------------------------------------------程序结束---------------------------------------------------
Sop("运行完毕,成功增加访问数:" + urlss.size());
//final int o=urlss.size();
//Sop("汇总:滴答……运行完毕,成功总增加:" +++shiji*urlss.size()+" 哇...我太牛了!!!");
Sop("汇总:滴答……运行完毕,成功总增加:" + (shiji+=urlss.size())+" 哇...我太牛了!!!");
//Sop(shiji+=urlss.size());
}
public static InputStream doGet(String urlstr) throws IOException {
URL url = new URL(urlstr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");
InputStream inputStream = conn.getInputStream();
return inputStream;
}
public static String inputStreamToString(InputStream is, String charset) throws IOException {
byte[] bytes = new byte[1024];
int byteLength = 0;
StringBuffer sb = new StringBuffer();
while ((byteLength = is.read(bytes)) != -1) {
sb.append(new String(bytes, 0, byteLength, charset));
}
return sb.toString();
}
// 正则匹配
public static List<String> getMatherSubstrs(String str, String regex) {
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
while (m.find()) {
list.add(m.group());
}
return list;
}
public static void Sop(Object obj){
System.out.println(obj); //封装打印,简化重复工作量
}
public static int shiji=0;
}
脚本运行如下图所示: