寫爬蟲和用Java寫爬蟲的區別是什么?
沒得區別的,用Java寫爬蟲代碼
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++);
}
}
}
}