在 java 中获取网页上某个树结构的某个节点的所有下级节点的 url,通常需要以下几个步骤:
- 下载网页内容(http 请求)。
- 解析 html 内容(使用 html 解析器)。
- 定位目标节点并提取其子节点的 url。
常见的实现方式包括:
- 使用 jsoup进行 html 解析。
- 使用 selenium进行动态页面处理(如果页面是通过 javascript 动态生成的)。
- 使用 httpclient+jsoup的组合进行静态页面解析。
下面分别介绍这几种方法,并提供对应的 maven 依赖和 java 示例代码
方法一:使用 jsoup 解析静态 html 页面
maven 依赖
<dependency>
    <groupid>org.jsoup</groupid>
    <artifactid>jsoup</artifactid>
    <version>1.16.1</version>
</dependency>
java 示例代码
假设你要从一个静态 html 页面中找到某个具有特定 id 的树节点,并提取它的所有子节点的 href 属性。
import org.jsoup.jsoup;
import org.jsoup.nodes.document;
import org.jsoup.nodes.element;
import org.jsoup.select.elements;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
public class treeurlparser {
    public static list<string> getchildurls(string url, string targetnodeid) throws ioexception {
        document doc = jsoup.connect(url).get();
        element targetnode = doc.getelementbyid(targetnodeid);
        list<string> childurls = new arraylist<>();
        if (targetnode != null) {
            elements links = targetnode.select("a[href]");
            for (element link : links) {
                string href = link.absurl("href"); // 获取绝对路径
                childurls.add(href);
            }
        }
        return childurls;
    }
    public static void main(string[] args) {
        try {
            list<string> urls = getchildurls("https://example.com/tree-page", "tree-node-id");
            urls.foreach(system.out::println);
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }
}
方法二:使用 selenium 处理动态加载的树结构
如果树结构是通过 javascript 动态加载的,则需要使用浏览器自动化工具如 selenium 来渲染页面。
maven 依赖
<dependency>
    <groupid>org.seleniumhq.selenium</groupid>
    <artifactid>selenium-java</artifactid>
    <version>4.19.0</version>
</dependency>
还需要下载 webdriver(如 chromedriver)。
java 示例代码
import org.openqa.selenium.by;
import org.openqa.selenium.webdriver;
import org.openqa.selenium.webelement;
import org.openqa.selenium.chrome.chromedriver;
import java.util.arraylist;
import java.util.list;
public class dynamictreeurlparser {
    public static list<string> getchildurls(string pageurl, string targetnodeid) {
        system.setproperty("webdriver.chrome.driver", "path/to/chromedriver");
        webdriver driver = new chromedriver();
        driver.get(pageurl);
        webelement targetnode = driver.findelement(by.id(targetnodeid));
        list<webelement> links = targetnode.findelements(by.tagname("a"));
        list<string> childurls = new arraylist<>();
        for (webelement link : links) {
            string href = link.getattribute("href");
            if (href != null && !href.isempty()) {
                childurls.add(href);
            }
        }
        driver.quit();
        return childurls;
    }
    public static void main(string[] args) {
        list<string> urls = getchildurls("https://example.com/dynamic-tree-page", "tree-node-id");
        urls.foreach(system.out::println);
    }
}
方法三:结合 apache httpclient 和 jsoup
如果你希望使用更底层的 http 客户端来控制请求细节,可以使用 httpclient。
maven 依赖
<dependency>
    <groupid>org.apache.httpcomponents.client5</groupid>
    <artifactid>httpclient5</artifactid>
    <version>5.2.1</version>
</dependency>
<dependency>
    <groupid>org.jsoup</groupid>
    <artifactid>jsoup</artifactid>
    <version>1.16.1</version>
</dependency>
java 示例代码
import org.apache.hc.client5.http.classic.methods.httpget;
import org.apache.hc.client5.http.impl.classic.closeablehttpclient;
import org.apache.hc.client5.http.impl.classic.httpclients;
import org.apache.hc.core5.http.classichttpresponse;
import org.apache.hc.core5.http.httpentity;
import org.jsoup.jsoup;
import org.jsoup.nodes.document;
import java.io.bufferedreader;
import java.io.inputstreamreader;
import java.util.arraylist;
import java.util.list;
public class httpclientjsoupparser {
    public static list<string> getchildurls(string url, string targetnodeid) throws exception {
        closeablehttpclient httpclient = httpclients.createdefault();
        httpget request = new httpget(url);
        classichttpresponse response = httpclient.execute(request);
        httpentity entity = response.getentity();
        list<string> childurls = new arraylist<>();
        if (entity != null) {
            bufferedreader reader = new bufferedreader(new inputstreamreader(entity.getcontent()));
            stringbuilder content = new stringbuilder();
            string line;
            while ((line = reader.readline()) != null) {
                content.append(line);
            }
            document doc = jsoup.parse(content.tostring());
            org.jsoup.nodes.element targetnode = doc.getelementbyid(targetnodeid);
            if (targetnode != null) {
                targetnode.select("a[href]").foreach(link -> {
                    childurls.add(link.absurl("href"));
                });
            }
        }
        return childurls;
    }
    public static void main(string[] args) {
        try {
            list<string> urls = getchildurls("https://example.com/tree-page", "tree-node-id");
            urls.foreach(system.out::println);
        } catch (exception e) {
            e.printstacktrace();
        }
    }
}
总结
| 方法 | 工具 | 是否支持动态内容 | 适用场景 | 
|---|---|---|---|
| 方法一 | jsoup | ❌ 不支持 | 静态 html 页面解析 | 
| 方法二 | selenium | ✅ 支持 | 动态 javascript 渲染页面 | 
| 方法三 | httpclient + jsoup | ❌ 不支持 | 更细粒度的 http 请求控制 | 
你可以根据实际需求选择合适的方法。
到此这篇关于java提取网页树节点url的三种高效方法的文章就介绍到这了,更多相关java提取网页树节点url内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
 
             我要评论
我要评论 
                                             
                                             
                                             
                                             
                                             
                                            
发表评论