本文以通过JAVA爬取某一网页下所有a标签href指向的链接为例,讲述org.jsoup对于HTML页面的使应用。
1、在pom.xml中配置依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>2、如果是爬取"http://www.xx.com/xx/xx"页面下的a标签href指向的链接,代码如下
public static List<String> getUrls(String urlStr) throws IOException {
List<String> result = new ArrayList<String>();
URL url = new URL(urlStr);
Document doc = Jsoup.parse(url, 10000);
Elements elements = doc.select("a");
elements.forEach((element) -> {
String href = element.attr("href");
result.add(href);
});
return result;
}
本文介绍如何利用Java中的org.jsoup库解析HTML并获取指定网页内所有a标签的href属性值,通过示例代码展示了从给定网址抓取链接的具体实现。
571

被折叠的 条评论
为什么被折叠?



