java爬虫爬取网络资源

news/2024/7/24 10:07:39 标签: 开发语言, java, 爬虫

要从网络爬取多个资源(压缩包)并将它们分别打包下载到本地目录,您可以使用Java中的以下步骤:

  1. 使用Java中的网络爬取库(如Jsoup)访问要爬取的网站并解析其内容以获取所有资源压缩包的链接。

  2. 创建一个本地目录,用于保存下载的压缩包。

  3. 使用Java中的ZipInputStream类打开每个下载的压缩包,并使用它来解压所有资源文件。

  4. 使用Java中的URLConnection类中的InputStream从网络中下载每个资源压缩包,并使用ZipInputStream解压缩每个文件并保存到本地目录中。

    <dependencies>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.14.3</version>
        </dependency>
    </dependencies>
     
    

以下是一个简单的Java代码示例,用于从网站上爬取多个压缩包文件并将它们分别解压缩到指定目录中:

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class WebCrawler {
    
    public static void main(String[] args) throws Exception {
        String siteUrl = "http://xxxx";
        String resourceType = "a";
        String localDir = "C:\\temp\\download";
        
        ArrayList<String> resourceUrls = getResourceUrls(siteUrl, resourceType);
        createLocalDir(localDir);
        downloadResources(resourceUrls, localDir);
    }
    
    //获取所有压缩包资源的链接
    public static ArrayList<String> getResourceUrls(String siteUrl, String resourceType) throws Exception {
        ArrayList<String> urls = new ArrayList<String>();
        Document doc = Jsoup.connect(siteUrl).get();
        Elements resources = doc.select(resourceType);
        for (Element resource : resources) {
            String url = resource.attr("abs:href");
            if (url.endsWith(".zip")) {
                urls.add(url);
            }
        }
        return urls;
    }
    
    //创建本地目录
    public static void createLocalDir(String localDir) {
        File dir = new File(localDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }
    
    //下载并解压缩资源文件
    public static void downloadResources(ArrayList<String> urls, String localDir) throws Exception {
        byte[] buffer = new byte[1024];
        for (String url : urls) {
            URL resourceUrl = new URL(url);
            URLConnection connection = resourceUrl.openConnection();
            InputStream is = connection.getInputStream();
            
            String fileName = url.substring(url.lastIndexOf("/") + 1);
            String filePath = localDir + "\\" + fileName;
            FileOutputStream fos = new FileOutputStream(filePath);
            int len;
            while ((len = is.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            is.close();
            fos.close();
            
            ZipInputStream zis = new ZipInputStream(new FileInputStream(filePath));
            ZipEntry ze = zis.getNextEntry();
            while (ze != null) {
                String entryName = ze.getName();
                String entryPath = localDir + "\\" + entryName;
                File entryFile = new File(entryPath);
                
                FileOutputStream entryFos = new FileOutputStream(entryFile);
                int entryLen;
                while ((entryLen = zis.read(buffer)) > 0) {
                    entryFos.write(buffer, 0, entryLen);
                }
                entryFos.close();
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        }
    }
}

该示例会从指定的网站上爬取所有压缩包文件的链接,并将它们分别解压缩到本地文件系统中指定的目录。注意,此示例假设所有资源文件都是压缩包,并且文件名以“.zip”结尾。如果网站上的资源文件不是压缩包,代码需要相应更改


http://www.niftyadmin.cn/n/4966381.html

相关文章

掌握AI助手的魔法工具:解密Prompt(提示)在AIGC时代的应用「中篇」

文章目录 掌握AI助手的魔法工具&#xff1a;解密Prompt&#xff08;提示&#xff09;在AIGC时代的应用「中篇」一、指南原则1: 使用明确和具体的指令原则2: 给模型思考的时间 二、迭代三、总结与提取四、局限与改善五、总结 掌握AI助手的魔法工具&#xff1a;解密Prompt&#x…

ubuntu20.04 编译安装运行emqx

文章目录 安装依赖编译运行登录dashboard压力测试 安装依赖 Erlang/OTP OTP 24 或 25 版本 apt-get install libncurses5-dev sudo apt-get install erlang如果安装的erlang版本小于24的话&#xff0c;可以使用如下方法自行编译erlang 1.源码获取 wget https://github.com/erla…

.NetCore之log4net的使用

1.首先下载log4ne的包&#xff1a; 2.添加配置文件log4net.config <?xml version"1.0" encoding"utf-8" ?> <configuration><!-- This section contains the log4net configuration settings --><log4net><appender name&q…

iOS 17 及 Xcode 15.0 Beta7 问题记录

1、iOS 17 真机调试问题 iOS 17之后&#xff0c;真机调试Beta版本必须使用Beta版本的Xcode来调试&#xff0c;用以前复制DeviceSupport 方式无法调试&#xff0c;新的Beta版本Xcode中&#xff0c;已经不包含 iOS 17目录。如下图&#xff1a; 解决方案&#xff1a; 1&#x…

ResNet18云空间部署

1-6步骤可以在云空间运行&#xff0c;也可以在本地运行&#xff1b;步骤7 在云空间运行。 1.编译ONNX模型 本章以 resnet18.onnx 为例, 介绍如何编译迁移一个onnx模型至BM1684X TPU平台运行。 该模型来自onnx的官网: models/vision/classification/resnet/model/resnet18-v1…

基于 VisualFoxPro 环境开发应用程序的过程

应用程序开发前开发者要与用户之间广泛沟通&#xff0c;作大量的调查研究和分析工 作&#xff0c;从而明确用户的要求、程序应具备的功能及可以完成的任务。为此要进行两方 面的分析&#xff0c;数据分析和功能分析。数据分析的目的是收集系统应包含的数据、数据 的真实性、…

Unity报错DllNotFoundException:sqlite3

Unity项目中要使用轻型数据库sqlite&#xff0c;除了导入sqlite3.dll外&#xff0c;还需要导入Mono.Data.Sqlite.dll和System.Data.dll&#xff08;工程里或者编辑器里面有System.Data.dll时就不需要&#xff09;两个文件。 如果在编辑器中运行出现 “DllNotFoundException:sql…

Ceph入门到精通-大流量10GB/s LVS+OSPF 高性能架构

LVS 和 LVSkeepalived 这两种架构在平时听得多了&#xff0c;最近才接触到另外一个架构LVSOSPF。这个架构实际上是LVSKeepalived 的升级版本&#xff0c;我们所知道LVSKeepalived 架构是这样子的&#xff1a; 随着业务的扩展&#xff0c;我们可以对web服务器做水平扩展&#xf…