ERLEDIGT
NEIN
NEIN
ANTWORTEN
7
7
ZUGRIFFE
1747
1747
EMPFEHLEN
-
06.03.12 11:04 #1
Rookie
- Registriert seit
- Mar 2012
- Beiträge
- 8
Die Aufgabe lautet :
1.Der Crawler lädt das HTML von einer URL
2.Er sucht die Link-URLs aus dem HTML (<a href=...>)
3.er lädt die URLs der gefundenen Links (zurück zu 1.)
4.Nach 10000 Pages bricht er ab.
1und 2 habe ich schon den Rest habe ich 2 tage daran gedacht und gesucht aber leider nichts gefunden was einfach ist ich bitte sie um Hilfe
voila mein code :
Code java: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
import java.net.URL; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import javax.swing.text.html.HTML; import javax.swing.text.html.HTMLDocument; import javax.swing.text.html.HTMLEditorKit; public class Java { public static List<String> addLinks(List<String> links,String urlLink){ links = new ArrayList<String>(); String line=""; HTMLEditorKit editorKit = new HTMLEditorKit(); HTMLDocument htmlDoc = new HTMLDocument(); try{ editorKit.read(new URL(urlLink).openStream(), htmlDoc, 0); }catch(Exception e){ e.getStackTrace(); } for(HTMLDocument.Iterator iter = htmlDoc.getIterator(HTML.Tag.A);iter.isValid();iter.next()) { line=(String)(iter.getAttributes().getAttribute(HTML.Attribute.HREF)); if (links.size()<10000){ //System.out.println(line); links.add(line); } } return links;} public static void startCrawler(int threads){ } public static void main(String[] args) { List <String> link = new ArrayList<String>(); addLinks(link,"http://www.tutorials.de"); System.out.println(link.size()); for(String element:link){ //hier habe ich ein problem dass die Liste leer bekomme System.out.println(element); // und wie geht es weiter } } }
Geändert von sheel (06.03.12 um 13:48 Uhr) Grund: Codetags
-
06.03.12 11:15 #2
Hallo,
ich würde an deiner Stelle Apache Nutch verwenden:
http://nutch.apache.org/
Btw. ein Beispiel zur Link Extrahierung haben wir auch:
http://www.tutorials.de/java/202949-...trahieren.html
Gruß TomJava rocks! http://www.jugsaar.de
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
06.03.12 11:29 #3
Rookie
- Registriert seit
- Mar 2012
- Beiträge
- 8
Danke für die schnelle Antwort mit der Aufgabe würde ich ein Praktikum platz bekommen unter der Bedienung dass ich dass Programm in java 6 mache und nur in eine classe ,ich habe dass gefunden http://www.uni-koblenz-landau.de/kob....Praxisaufgabe
warum in main mein liste ist leer ,obwohl habe ich ein Methode um es auszufüllen (siehe Kommentare),bitte Thomson ich habe gesehen dass du drauf hast bitte hilf mir um mein studium abzuschliessen
Danke ,vielen Dank
lg
-
06.03.12 12:31 #4
Hi,
also ich habe bisher auch noch keinen Crawler geschrieben. Allerdings wollte ich das selber schon seit längerem mal ausprobieren. Perfekt ist er vielleicht nicht, aber er erfüllt seinen Zweck.
Code java: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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
package de.tutorials; 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.List; import java.util.Set; import java.util.TreeSet; import javax.swing.text.MutableAttributeSet; import javax.swing.text.html.HTML; import javax.swing.text.html.HTML.Tag; import javax.swing.text.html.HTMLEditorKit.ParserCallback; import javax.swing.text.html.parser.ParserDelegator; /** * @author FabioH * @author $Author$ * @version $Revision$ $Date$ */ public class UrlCrawler { private Thread crawlerThread; /** * */ public UrlCrawler() { } /** * @param proxyHost * @param proxyPort */ public UrlCrawler(final String proxyHost, final int proxyPort) { System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", String.valueOf(proxyPort)); } /** * @param url * @param maxUrls */ public void start(final String url, final int maxUrls) { if(crawlerThread == null) { try { crawlerThread = new Thread(new RecursiveCrawler(url, maxUrls), "Crawler"); crawlerThread.start(); } catch(final MalformedURLException e) { e.printStackTrace(); } } } /** * */ public void stop() { if(crawlerThread != null) { crawlerThread.interrupt(); crawlerThread = null; } } private final class RecursiveCrawler implements Runnable { private final URL url; private final Set<String> alreadyParsedUrls = new TreeSet<String>(); private int counter = 0; private final int maxUrls; /** * @param urlStr * @param maxUrls * @throws MalformedURLException */ public RecursiveCrawler(final String urlStr, final int maxUrls) throws MalformedURLException { this.maxUrls = maxUrls; url = new URL(urlStr); } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override public void run() { crawl(url); } /** * @param crawlUrl */ private void crawl(final URL crawlUrl) { if(maxUrls < counter && alreadyParsedUrls.add(crawlUrl.toString())) { final HtmlParser parser = new HtmlParser(); final List<URL> allHyperlinksFromUrl = parser.getAllHyperlinksFromUrl(crawlUrl); counter++; System.out.println(counter + ".\t Crawl: " + crawlUrl + ", Hyperlinks found=" + allHyperlinksFromUrl.size()); for(final URL url : allHyperlinksFromUrl) { crawl(url); } } } } private class HtmlParser extends ParserCallback { private List<URL> hyperlinks; private String host; /** * @param url * @return a list with all hyperlinks from the delivered url */ public List<URL> getAllHyperlinksFromUrl(final URL url) { host = url.getHost(); hyperlinks = new ArrayList<URL>(); InputStream in = null; InputStreamReader reader = null; try { in = url.openStream(); reader = new InputStreamReader(in); new ParserDelegator().parse(reader, this, true); } catch(final MalformedURLException e) { // Ignore?! } catch(final IOException e) { // Ignore?! } finally { try { if(reader != null) reader.close(); } catch(final IOException e) { e.printStackTrace(); } try { if(in != null) in.close(); } catch(final IOException e) { e.printStackTrace(); } } return hyperlinks; } /* * (non-Javadoc) * * @see * javax.swing.text.html.HTMLEditorKit.ParserCallback#handleStartTag(javax.swing.text.html * .HTML.Tag, javax.swing.text.MutableAttributeSet, int) */ @Override public void handleStartTag(final Tag t, final MutableAttributeSet a, final int pos) { if(Tag.A == t) { final Object attributeHref = a.getAttribute(HTML.Attribute.HREF); if(attributeHref != null) { String href = attributeHref.toString(); if(href.startsWith("/")) { href = host + href; } try { hyperlinks.add(new URL(href)); } catch(final MalformedURLException e) { // Ignore?! } } } } } /** * @param args */ public static void main(final String[] args) { final UrlCrawler crawler = new UrlCrawler(); crawler.start("http://www.tutorials.de/", 50); } }
Gruß
FabioGeändert von Fabio Hellmann (06.03.12 um 15:26 Uhr) Grund: kleine Codeänderung
Bitte die Code-Tags verwenden. Bei Java-Code: [java]...[/java]
Tutorials:
Automatisches erzeugen eines Inhaltsverzeichnisses (Javascript)
JAnimationPanel - Animationen für Swing/AWT
SWTRatingBar (Bewertungs-Composite) selbst programmieren
____________________________________________________________________________
Über eine Bewertung (Stern links unter dem Beitrag) oder ein Danke freue ich mich sehr.
-
06.03.12 15:58 #5
Mitglied Smaragd
- Registriert seit
- Jun 2009
- Beiträge
- 1.020
Der Crawler von Fabio berücksichtigt aber nur das Attribut HREF. Bilder, Stylesheets, Scripte, Objekte, Videos, … werden ignoriert.
Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.Code java:1
System.out.println("Hallo");
-
06.03.12 17:41 #6
Ja das ist klar. Aber Bilder, Stylesheets, etc. noch mit einzubinden ist jetzt nicht mehr weiter schwer. Das ist schließlich nur ein Grundgerüst.
Bitte die Code-Tags verwenden. Bei Java-Code: [java]...[/java]
Tutorials:
Automatisches erzeugen eines Inhaltsverzeichnisses (Javascript)
JAnimationPanel - Animationen für Swing/AWT
SWTRatingBar (Bewertungs-Composite) selbst programmieren
____________________________________________________________________________
Über eine Bewertung (Stern links unter dem Beitrag) oder ein Danke freue ich mich sehr.
-
07.03.12 20:16 #7
Rookie
- Registriert seit
- Mar 2012
- Beiträge
- 8
ich habe es doch gelöst hier ist die lösung.
Code java: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
import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.swing.text.html.*; public class Aufgabe { /** * @param args */ //Methode füllt eine List mit Urls auf public static List <String> addUrls(String urlLink){ List<String>list=new ArrayList<String>(); String line=""; HTMLEditorKit editorKit = new HTMLEditorKit(); HTMLDocument htmlDoc = new HTMLDocument(); htmlDoc.putProperty("IgnoreCharsetDirective", Boolean.TRUE); try{ editorKit.read(new URL(urlLink).openStream(), htmlDoc, 0); }catch(Exception e){ e.getStackTrace(); } for(HTMLDocument.Iterator iter = htmlDoc.getIterator(HTML.Tag.A);iter.isValid();iter.next()){ line=(String)(iter.getAttributes().getAttribute(HTML.Attribute.HREF)); if((line!=null)&&(list.indexOf(line)==-1)){ list.add(line); } } return list; } //Metohde füllt die Ergebnisliste mit den gefundenen Listen von Urls public static List<String> addList(String url,List<String>urlList){ int i=0; int counter=0; urlList.addAll(addUrls(url)); while(urlList.size()<=10000){ int j=0; for(String e:addUrls(urlList.get(i))){ System.out.println(counter+" : "+e); urlList.add(addUrls(urlList.get(i)).get(j)); counter++; j++; } i++; } return urlList; } public static void main(String[] args) { List <String> allLinks=new ArrayList<String>(); addList("http://www.der-webdesigner.net",allLinks); int i=1; for(String e:allLinks){ System.out.println(i+": "+e); i++; } } }
-
07.03.12 20:34 #8
Mitglied Smaragd
- Registriert seit
- Jun 2009
- Beiträge
- 1.020
Kannst du den Code bitte in Java-Tags fassen ? (siehe meine Signatur)
Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.Code java:1
System.out.println("Hallo");
Ähnliche Themen
-
C++ Crawler
Von fabianh im Forum C/C++Antworten: 0Letzter Beitrag: 25.08.10, 16:49 -
Website Crawler
Von lajilla im Forum HTML & XHTMLAntworten: 2Letzter Beitrag: 28.07.10, 15:41 -
Caliper schlankes mini-Framework für Java Microbenchmarks
Von Thomas Darimont im Forum JavaAntworten: 0Letzter Beitrag: 12.06.10, 01:02 -
SEO und Crawler
Von Webgau im Forum PHPAntworten: 5Letzter Beitrag: 09.01.09, 12:38 -
mini mini Problemchen - zefix
Von Fexxx im Forum Javascript & AjaxAntworten: 2Letzter Beitrag: 26.04.05, 20:43




Zitieren

Login