1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
private static Pattern pattern = Pattern.compile("<\\s*a\\s*href\\s*=\\s*\"\\s*(.*?)\"\\s*>(.*?)<\\s*/\\s*a\\s*>");
private Matcher matcher;
private static ArrayList<String>a=new ArrayList<String>();
private static ArrayList<String>passe=new ArrayList<String>();
private String unLienprincipale;
public Test(String lienprincipale){
this.unLienprincipale=lienprincipale;
try {
this.lecture(this.unLienprincipale);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(String n:a){
System.out.println(n);
}
}
public static void main(String[] arg){
new Test("http://www.monsite.com");
}
public void lecture(String lien) throws MalformedURLException {
this.passe.add(lien);
String ligne="";
String contenu="";
if(urlExists(lien)){
URL url=new URL(lien);
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
try {
while ((ligne = br.readLine()) != null) {
contenu=contenu+ligne;
}
br.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
matcher=pattern.matcher(contenu);
while(matcher.find()){
String lien2=matcher.group(1);
if(!this.passe.contains(lien2) || !this.a.contains(lien2)){
this.lecture(lien2);}
}
}else{
System.out.println(lien);
a.add(lien);
}
}
public boolean urlExists(String url)
{
try {
URL site = new URL(url);
try {
site.openStream();
return true;
} catch (IOException ex) {
return false;
}
} catch (MalformedURLException ex) {
return false;
}
}
} |
Partager