java写爬虫的代码 java爬虫jsoup( 二 )


如何用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 = "-";
Pattern reg = Pattern.compile("img src=https://www.04ip.com/"(.*?)\"");
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++);
}
}
}
}
java写爬虫的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java爬虫jsoup、java写爬虫的代码的信息别忘了在本站进行查找喔 。

推荐阅读