最新Java~使用URL建立资源连接实现网络图片爬取,java栈和堆面试

最后

本人也收藏了一份Java面试核心知识点来应付面试,借着这次机会可以送给我的读者朋友们:

目录:

二面蚂蚁金服(交叉面),已拿offer,Java岗定级阿里P6

Java面试核心知识点

一共有30个专题,足够读者朋友们应付面试啦,也节省朋友们去到处搜刮资料自己整理的时间!

二面蚂蚁金服(交叉面),已拿offer,Java岗定级阿里P6

Java面试核心知识点

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

resourceGet(file, keyWords);

}

} else {

resourceGet(file, keyWords);

}

}

private void resourceGet(File file, String keyWords) {

InputStream inputStream = null;

FileOutputStream fileOutputStream = null;

try {

//创建url连接

URL url = new URL(“https://image.baidu.com/search/index” +

“?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=” + keyWords);

//打开资源

inputStream = url.openStream();

//字节数组用于临时存储资源

byte[] buffer = new byte[1024];

//使用StringBuilder保存获取的所有资源

StringBuilder allResource = new StringBuilder();

int len = -1;

while ((len = inputStream.read(buffer)) != -1) {

//获取到了资源

allResource.append(new String(buffer, 0, len, StandardCharsets.UTF_8));

}

//使用图片资源的正则表达式获取我们想要的资源

String allPictureResource = allResource.toString();

Pattern compile = Pattern.compile(“https://.*?0\.jpg”);

//使用匹配器匹配我们想要的资源

Matcher matcher = compile.matcher(allPictureResource);

//查看是否有匹配到的东西 如果有就获取该资源

List haveDown = new ArrayList<>();

int count = 1;

System.out.println(“开始爬取…”);

while (matcher.find() ) {

//System.out.println(“正在爬取第” + count++ + “张…”);

System.out.println(“正在爬取ing…”);

//获取匹配到的一条结果

String pictureResource = matcher.group();

//如果这个资源的照片已经下载过就不进行再次下载

if (haveDown.contains(pictureResource)) {

continue;

}

//与这个图片资源获得连接

URL pictureURL = new URL(pictureResource);

inputStream = pictureURL.openStream();

//创建文件输出流

fileOutputStream = new FileOutputStream(file.getPath() + “/” + System.currentTimeMillis() + “.jpg”);

//输入到文件中

while ((len = inputStream.read(buffer)) != -1) {

fileOutputStream.write(buffer, 0, len);

}

//关闭资源

fileOutputStream.flush();

fileOutputStream.close();

inputStream.close();

haveDown.add(pictureResource);

//System.out.println(“爬取第” + count++ + “张完毕…”);

}

} catch (IOException e) {

e.printStackTrace();

System.out.println(“爬取失败…”);

} finally {

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fileOutputStream != null) {

try {

fileOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

System.out.println(“爬取成功…”);

}

}

}

演示


import java.util.Scanner;

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-09-02

  • Time: 22:02

*/

public class Test {

感受:

其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。

特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。

也祝愿各位同学,都能找到自己心动的offer。

分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档

拿到字节跳动offer后,简历被阿里捞了起来,二面迎来了P9"盘问"

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

-jidnXqv7-1715631761924)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你回答这个问题。以下是使用Java语言编写网络爬虫爬取网页文字和图片的业务代码示例: ``` import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.*; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class WebCrawler { private static final Pattern pattern = Pattern.compile("([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)"); public static void main(String[] args) { String url = "https://www.example.com"; crawl(url); } private static void crawl(String url) { try { Document doc = Jsoup.connect(url).get(); String content = doc.text(); saveToFile(content, "content.txt"); Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]"); for (Element image : images) { String imageUrl = image.attr("src"); saveImage(imageUrl); } } catch (IOException e) { e.printStackTrace(); } } private static void saveToFile(String content, String fileName) { try (FileWriter writer = new FileWriter(fileName)) { writer.write(content); } catch (IOException e) { e.printStackTrace(); } } private static void saveImage(String imageUrl) { try { Matcher matcher = pattern.matcher(imageUrl); if (matcher.find()) { URL url = new URL(imageUrl); InputStream inputStream = url.openStream(); File file = new File(matcher.group()); OutputStream outputStream = new FileOutputStream(file); byte[] buffer = new byte[2048]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } ``` 这个代码可以爬取指定URL上的网页文字和图片,并将文字保存到文件 content.txt 中,将图片保存到当前目录下以图片URL中的文件名为文件名的文件中。 需要注意的是,这个代码只是一个简单的示例,实际应用中需要考虑更多的异常处理、多线程爬取、重试等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值