
package game.Worm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class InternetWorm {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入初始爬取网址入口:");
String address = scanner.next();
System.out.print("请输入想要爬取的内容(0代表全部爬取):");
String content = scanner.next();
new Thread(new WormWebsite(address,content)).start();
}
}
class WormWebsite implements Runnable {
String content;
URL url;
public WormWebsite(String address, String content) {
try {
this.url = new URL(address);
this.content = content;
} catch (MalformedURLException e) {
}
}
@Override
public void run() {
if (url == null) {
return;
}
URLConnection urlConnection = null;
try {
urlConnection = url.openConnection();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (urlConnection == null) {
return;
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
String regex1 = "https://[^\"']+";
Pattern pattern = Pattern.compile(regex1);
while ((line = bufferedReader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
new Thread(new WormWebsite(matcher.group(),content)).start();
Thread.sleep(1000);
new Thread(new WormComment(matcher.group(),content)).start();
Thread.sleep(1000);
}
}
} catch (Exception e) {
}
}
}
class WormComment implements Runnable {
String content;
URL url;
public WormComment(String address, String content) {
try {
this.url = new URL(address);
this.content = content;
} catch (MalformedURLException e) {
}
}
@Override
public void run() {
if (url == null) {
return;
}
URLConnection urlConnection = null;
try {
urlConnection = url.openConnection();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (urlConnection == null) {
return;
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
unicode.append("\\u").append(Integer.toHexString(c));
}
String regex2 = "[^"+unicode.toString()+"]*"+unicode.toString()+".*";
if (content.equals("0")){
regex2 = " .* ";
}
Pattern pattern = Pattern.compile(regex2);
while ((line = bufferedReader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
Thread.sleep(50);
System.out.println(matcher.group());
}
}
} catch (Exception e) {
}
}
}