Verwenden Sie Java, um alle Bilder auf der Webseite zu greifen:
Verwenden Sie zwei reguläre Ausdrücke:
1. Die reguläre Übereinstimmung mit dem IMG -Tag in html: <img.*Src = (.*?) [^>]*?>
2. Übereinstimmen Sie den HTTP -Pfad in SRC im IMG -Tag: http:/"? (.*?) (/" |> | // s+)
erreichen:
Paket org.swinglife.main; Import Java.io.file; importieren java.io.fileoutputStream; importieren java.io.inputstream; importieren java.net.url; importieren java.net.urlConnection; Import Java.util.ArrayList; importieren java.util.list; Import Java.util.Regx.Matcher; Import Java.util.Regex.Pattern; /**** Java Crawl Network Images* @Author SwingLife**/öffentliche Klasse Catchimage {// Adresse private statische endgültige String -String -url = "http://www.csdn.net"; // codieren private statische endgültige Zeichenfolge ecoding = "utf-8"; // das IMG -Tag reguliert regelmäßig private statische endgültige Zeichenfolge imgurl_reg = "<img.*Src = (.*?) [^>]*?>"; // den SRC -Pfad reguläre private statische endgültige Zeichenfolge imgsrc_reg = "http:/"? (.*?) (/"|> | // s+)" erhalten; public static void main (String [] args) löst eine Ausnahme aus {catchimage cm = new Catchimage (); // HTML -Textinhaltszeichenfolge html = cm.gethtml (URL) abrufen; // Die Bild -Tag -Liste <string> imgurl = cm.getImageurl (html) abrufen; // Die Bild -SRC -Adressliste <string> imgsrc = cm.getImagesrc (imgurl) abrufen; // das Bild cm.download (IMGSRC) herunterladen; } / *** * HTML -Inhalt abrufen * * @param url * @return * @throws Exception * / private String Gethtml (String url) löst Ausnahme aus {url uri = new url (url); UrlConnection Connection = Uri.OpenConnection (); InputStream in = connection.getInputStream (); byte [] buf = neues byte [1024]; int länge = 0; StringBuffer sb = new StringBuffer (); while ((länge = in.read (buf, 0, buf.length))> 0) {sb.append (neue String (buf, ecoding)); } in.close (); return sb.tostring (); } / *** * ImageRl -Adresse erhalten * * @param html * @return * / private list <string> getImageUrl (String html) {Matcher Matcher = muster.comPile (imgurl_reg) .Matcher (html); List <String> listimgurl = new ArrayList <string> (); while (matcher.find ()) {listimgurl.add (matcher.group ()); } return listimgurl; } / *** * Bilderrc -Adresse erhalten * * @param listimageurl * @return * / private list <string> getImagesrc (list <string> listimageurl) {list <string> listimgsrc = new ArrayList <string> (); für (String -Bild: listimageUrl) {Matcher Matcher = muster.comPile (imgsrc_reg) .Matcher (Bild); while (matcher.find ()) {listimgsrc.add (matcher.group (). substring (0, matcher.group (). Länge () - 1)); }} return listimgsrc; }/**** Bild herunterladen** @param listimgsrc*/private void download (list <string> listimgsrc) {try {for (String url: listimgsrc) {String bildename = url.substring (url.lastindexof ("/") + 1, url.Length (); URL URI = neue URL (URL); InputStream in = uri.openstream (); FileOutputStream fo = new FileOutputStream (neue Datei (Bildname)); byte [] buf = neues byte [1024]; int länge = 0; System.out.println ("Start Download:" + URL); while ((länge = in.read (buf, 0, buf.length))! = -1) {fo.write (buf, 0, Länge); } in.close (); fo.close (); System.out.println (ImageName + "Download complete"); }} catch (Ausnahme e) {System.out.println ("Download fehlgeschlagen"); }}}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.