2/2
AnalysePageWeb.java
import java.io.*;
import java.net.*;
class WebPage {
URL URLcourant;
int nboctets;
int nb_href;
public WebPage(String u) {
try {URLcourant = new URL(u);}
catch (MalformedURLException e) {
Terminal.ecrireString("PB URL");
}
nboctets=0;
nb_href=0;
}
public void etude() {
char caractCourant;
String baliseCourante;
try {
InputStream uuu = URLcourant.openStream();
DataInputStream donneesHTML = new DataInputStream(uuu);
while (true) {
caractCourant = (char) donneesHTML.readByte();
nboctets++;
if (caractCourant == '<') {
baliseCourante = analyseBalise(donneesHTML);
if (baliseCourante.startsWith("<A HREF") ||
baliseCourante.startsWith("<a href")) {
Terminal.ecrireStringln(baliseCourante);
nb_href++;
}
}
}
}
catch (EOFException e) {
Terminal.ecrireStringln("TERMINE: Taille = "+nboctets+" NB HREF = "+nb_href);
}
catch (IOException e) {
System.out.println(e);
}
}
public String analyseBalise(DataInputStream is) {
StringBuffer baliseCourante = new StringBuffer("<");
char caractCourant = '<';
try {
while (caractCourant != '>') {
caractCourant = (char) is.readByte();
baliseCourante.append(caractCourant);
nboctets++;
}
}
catch(EOFException e) {
Terminal.ecrireStringln("PB fin page");
}
catch(Exception e) {
System.err.println(e);
}
return baliseCourante.toString();
}
}
public class AnalysePageWeb {
public static void main(String[] args) {
WebPage pg = new WebPage("http://cnam37.free.fr");
pg.etude();
}
}