Hallo zusammen,
ich habe annaehernd das, was ich gewuenscht hatte durch einen befreundeten Programmierer umgesetzt bekommen und schicke hiermit den Code, denn vielleicht kann jemand detaillerter beschreiben, was genau dort gemacht wurde. Denn ich habe mir zwar einige Notizen gemacht und er hat ja auch einige Kommentare geschrieben, ich als Javaneuling verstehe dennoch trotzdem wenig, was dort im Detail gemacht wurde. So bin ich mir jetzt selbst ueber die Quelle nicht mehr sicher, er muesste eine Library von Jsoup implementiert haben, es koennte die commons-io-2.4-bin.zip sein, weil ich diese gerade noch auf dem Rechner finde. Naja aber auch sonst eine einfache Erklaerung zu den Zeilen wuerde mir stark helfen, es ist ja nicht besonders viel Code, bis jetzt weiss ich nur, dass bei der Ausgabe 4 Elemente einer Webseite gezaehlt werden. Woerter, Bilder, Links und Eingabefelder, aber auf welcher Grundlage dies beruht wuerde ich gerne auch noch verstehen. Ich weiss nur noch, dass er meinte, dass es schwierig ist zu definieren, was nun genau ein Bild auf einer Webseite ist, also zaehlen auch Buttons dazu oder auch kleine Icons, das selbe bei den Worten, wird nur allgemeiner Text gezaehlt oder jedes einzelne Wort. Nun ja vielleicht kann ja jemand diesen Code in einfache Worte fassen, wuerde mich retten, vielen Dank schon mal!!
Hier der Code:
package org.jsoup.examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor;
/**
* HTML to plain-text. This example program demonstrates the use of jsoup to convert HTML input to lightly-formatted
* plain-text. That is divergent from the general goal of jsoup's .text() methods, which is to get clean data from a
* scrape.
* <p/>
* Note that this is a fairly simplistic formatter -- for real world use you'll want to embrace and extend.
*
* @author Jonathan Hedley, [email]jonathan@hedley.net[/email]
*/
public class HtmlTextExtractor {
private static final String HTRACK_INDEX_END_MARKER = "\"";
private static final String HTTRACK_INDEX_NAME = "index.html";
private static final String HTTRACK_INDEX_START_MARKER = "URL=";
public static void main(String... args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply path to process");
String dir = args[0];
File repo = new File(dir);
System.out.println("Website,Words,Links,Images,Inputs");
for(String name:repo.list()) {
if (new File(repo, name).isDirectory()) {
oneWebsite(repo, name);
}
}
}
private static void oneWebsite(File repo, String name) throws IOException {
File dir = new File(repo, name);
try {
File indexFile = getIndexPath(dir);
Document doc = Jsoup.parse(indexFile, null);
if (isFrameset(doc)) {
System.out.printf("%s,Frameset
", name);
} else {
HtmlTextExtractor formatter = new HtmlTextExtractor();
String plainText = formatter.getPlainText(doc);
System.out.printf("%s,%d,%d,%d,%d
", name,
countWords(plainText),
countLinks(doc),
countImages(doc),
countInputFields(doc));
}
} catch (FileNotFoundException e) {
System.out.printf("%s,No httrac index file
", name);
}
}
private static File getIndexPath(File dir) throws IOException, FileNotFoundException {
File httrackIndex = new File(dir, HTTRACK_INDEX_NAME);
FileInputStream inputStream = new FileInputStream(httrackIndex);
try {
String everything = IOUtils.toString(inputStream);
int urlIndex = everything.indexOf(HTTRACK_INDEX_START_MARKER) + HTTRACK_INDEX_START_MARKER.length();
int urlLastIndex = everything.indexOf(HTRACK_INDEX_END_MARKER, urlIndex);
return new File(dir, everything.substring(urlIndex, urlLastIndex));
} finally {
inputStream.close();
}
}
private static boolean isFrameset(Document doc) {
Elements framesets = doc.select("frameset");
return framesets.size() > 0;
}
private static int countWords(String text) {
boolean inWord = false;
int count = 0;
int len = text.length();
for (int i = 0; i < len; i++) {
final char c = text.charAt(i);
switch (c) {
case ' ':
case ' ':
case '
':
if (inWord) {
count++;
}
inWord = false;
default:
if (Character.isLetterOrDigit(c)) {
inWord = true;
}
}
}
return count;
}
private static int countLinks(Document doc) {
Elements links = doc.select("a[href]");
return links.size();
}
private static int countImages(Document doc) {
Elements images = doc.select("img[src]");
return images.size();
}
private static int countInputFields(Document doc) {
// we only count inputs of type text and textareas, as they account for
// 99.9% of what we want
Elements textInputs = doc.select("input[type=text]");
Elements textareas = doc.select("textarea");
return textInputs.size() + textareas.size();
}
/**
* Format an Element to plain-text
* @param element the root element to format
* @return formatted text
*/
public String getPlainText(Element element) {
FormattingVisitor formatter = new FormattingVisitor();
NodeTraversor traversor = new NodeTraversor(formatter);
traversor.traverse(element); // walk the DOM, and call .head() and .tail() for each node
return formatter.toString();
}
// the formatting rules, implemented in a breadth-first DOM traverse
private class FormattingVisitor implements NodeVisitor {
private static final int maxWidth = 80;
private int width = 0;
private StringBuilder accum = new StringBuilder(); // holds the accumulated text
// hit when the node is first seen
public void head(Node node, int depth) {
String name = node.nodeName();
if (node instanceof TextNode)
append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
}
// hit when all of the node's children (if any) have been visited
public void tail(Node node, int depth) {
String name = node.nodeName();
if (name.equals("br"))
append("
");
else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5"))
append("
");
}
// appends text to the string builder with a simple word wrap method
private void append(String text) {
accum.append(text + " ");
}
public String toString() {
return accum.toString();
}
}
}