Utilisez Java pour saisir toutes les images sur la page Web:
Utilisez deux expressions régulières:
1. L'appariement régulier de la balise IMG en html: <img. * Src = (. *?) [^>] *?>
2. Faites correspondre le chemin HTTP dans SRC dans la balise IMG: http: / "? (. *?) (/" |> | // s +)
accomplir:
package org.swinglife.main; Importer java.io.file; Importer java.io.fileOutputStream; Importer java.io.inputStream; Importer java.net.url; import java.net.urlconnection; import java.util.arraylist; Importer java.util.list; import java.util.regex.matcher; import java.util.regex.pattern; / *** * Java Crawl Network Images * @Author SwingLife * * / public class CatchImage {// Address Private Static Final String url = "http://www.csdn.net"; // Encodage de chaîne finale statique privée ecoding = "utf-8"; // Obtenez la chaîne finale statique privée IMG régulière imgurl_reg = "<img. * Src = (. *?) [^>] *?>"; // Obtenez la chaîne finale Src Path Src STATIQUE régulière imgsrc_reg = "http: /"? (. *?) (/ "|> | // s +)"; public static void main (String [] args) lève une exception {catchImage cm = new CatchImage (); // Obtenez la chaîne de contenu de texte html html = cm.gethtml (URL); // Obtenez la liste des balises d'image <string> imgurl = cm.getImageUrl (html); // Obtenez la liste d'adresses SRC d'image <string> imgsrc = cm.getImagesrc (imgurl); // Téléchargez l'image cm.Download (IMGSRC); } / *** * Obtenez le contenu html * * @param url * @return * @throws exception * / private String gethtml (String url) lève l'exception {url uri = new URL (url); Connexion UrlConnection = URI.OpenConnection (); InputStream dans = connection.getInputStream (); octet [] buf = nouveau octet [1024]; int length = 0; StringBuffer sb = new StringBuffer (); while ((longueur = in.read (buf, 0, buf.length))> 0) {sb.append (new String (buf, ecoding)); } in.close (); return sb.toString (); } / *** * Obtenez l'adresse ImageUrl * * @param html * @return * / list private <string> getImageUrl (String html) {Matcher Matcher = Pattern.Compile (imgurl_reg) .matcher (html); List <string> listImGurl = new ArrayList <string> (); while (Matcher.Find ()) {listImgurl.add (Matcher.Group ()); } return listImgurl; } / *** * Obtenez l'adresse ImagesRc * * @param listImageUrl * @return * / private list <string> getImagesrc (list <string> listImageUrl) {list <string> listImgsrc = new ArrayList <string> (); for (String Image: listImageUrl) {Matcher Matcher = Pattern.Compile (imgsrc_reg) .matcher (image); while (Matcher.Find ()) {listImgsrc.Add (Matcher.Group (). substring (0, Matcher.Group (). Length () - 1)); }} return listImgsrc; } / *** * Télécharger l'image * * @param listImgsrc * / private void download (list <string> listImgsrc) {try {for (String url: listImgsrc) {String ImageName = url.Length (); URL URI = nouvelle URL (URL); InputStream dans = uri.openStream (); FileOutputStream fo = new FileOutputStream (nouveau fichier (ImageName)); octet [] buf = nouveau octet [1024]; int length = 0; System.out.println ("Démarrer Télécharger:" + URL); while ((longueur = in.read (buf, 0, buf.length))! = -1) {fo.write (buf, 0, longueur); } in.close (); fo.close (); System.out.println (ImageName + "Download complet"); }} catch (exception e) {System.out.println ("téléchargement échoué"); }}}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.