如何用Java写一个爬虫
展开全部
import java.io.File;import java.net.URL;import java.net.URLConnection;import java.nio.file.Files;import java.nio.file.Paths;import java.util.Scanner;import java.util.UUID;import java.util.regex.Matcher;import java.util.regex.Pattern; public class DownMM { public static void main(String[] args) throws Exception { //out为输出的路径,注意要以\\结尾 String out = "D:\\JSP\\pic\\java\\"; try{ File f = new File(out); if(! f.exists()) { f.mkdirs(); } }catch(Exception e){ System.out.println("no"); } String url = "http://www.mzitu.com/share/comment-page-"; Pattern reg = Pattern.compile("<img src=\"(.*?)\""); for(int j=0, i=1; i<=10; i++){ URL uu = new URL(url+i); URLConnection conn = uu.openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"); Scanner sc = new Scanner(conn.getInputStream()); Matcher m = reg.matcher(sc.useDelimiter("\\A").next()); while(m.find()){ Files.copy(new URL(m.group(1)).openStream(), Paths.get(out + UUID.randomUUID() + ".jpg")); System.out.println("已下载:"+j++); } } }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询