抓取网页数据、下载网络图片

本文介绍如何利用JSoup库从指定网址抓取网页数据,并通过自定义方法下载网页中的图片到本地。包括URL构建、连接建立、图片元素定位及下载流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过jsoup抓取网页数数据<pre name="code" class="java">Document doc = Jsoup.connect("http://www.chinau.cc/cata/detail?cid=2060").timeout(5000000).get();
Elements url = doc.select(".content .dleft .x_img").select("tbody td img");
//就想像jquery那样去找到对应的元素(Element)或者多个元素(Elements)去做相应的操作即可
//attr(..),text(),html()对应这jquery的方法基本都有 
<pre code_snippet_id="402777" snippet_file_name="blog_20140623_7_6691604" name="code" class="java">

下载网络图片
/** * 下载网络图片 * @param url 网络路径 * @param path 本地保存路径 * @param fileName 保存文件名 * @throws IOException * */public static void downUrl(String urlStr,String path,String fileName) throws IOException{URL url = new URL(urlStr); //构建URLURLConnection con = url.openConnection(); //打开连接con.setConnectTimeout(5*10*1000);//设置超时连接//读入网络图片流InputStream inStream = con.getInputStream();File file = new File(path);//保存路径if (!file.exists()) {file.mkdirs();}//图片输出流OutputStream outStream = new FileOutputStream(file+"\\"+fileName);byte[] b = new byte[1024];int len = 0 ;while ((len = inStream.read(b)) != -1) {outStream.write(b, 0, len);}//关闭数据流outStream.close();inStream.close();}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值