java遍历链接_Java Jsoup解析遍历HTML文档中所有本站的链接地址

相关:http://www.what21.com/sys/view/java_component_1481611781290.html

代码案例:package com.what21.jsoup.a;

import java.io.IOException;

import org.jsoup.Connection;

import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;

import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;

public class JsoupImage {

public static void main(String[] args) throws IOException{

// 返回HTML的站点URL

String httpUrl = "http://www.what21.com";

// 创建连接

Connection conn = Jsoup.connect(httpUrl);

// 返回文档对象

Document doc = conn.get();

// 返回所有的Element

Elements eles = doc.getAllElements();

// 遍历所有的文档

for(Element ele : eles){

String tagName = ele.tagName();

if("a".equalsIgnoreCase(tagName)){

// 链接地址

String href = ele.attr("abs:href");

if(href.startsWith(httpUrl)){

System.out.println(href);

}

}

}

}

}

输出内容:http://www.what21.com/search/index.html

http://www.what21.com/search/index.html

http://www.what21.com/index.html

http://www.what21.com/a/src.html

http://www.what21.com/a/it.html

http://www.what21.com/a/book.html

http://www.what21.com/c/view/java.html

http://www.what21.com/sys/index.html

http://www.what21.com/article/index.html

http://www.what21.com/a/index.html

http://www.what21.com/sys/index.html

http://www.what21.com/search/index.html

http://www.what21.com/a/search.html

http://www.what21.com/a/src_j2ee_1464583163935.html

http://www.what21.com/a/src_ui_1463211852820.html

http://www.what21.com/a/src_java_1462640583454.html

http://www.what21.com/a/book_2_1462983656981.html

http://www.what21.com/a/src_java_1464843498044.html

http://www.what21.com/a/book_5_1462769489587.html

http://www.what21.com/a/book_4_1465532916693.html

http://www.what21.com/a/book_5_1462982876975.html

http://www.what21.com/a/book_1_1462760886361.html

http://www.what21.com/a/search.html

http://www.what21.com/a/search.html

http://www.what21.com/a/src_j2ee_1475575804319.html

http://www.what21.com/a/src_java_1467351251205.html

http://www.what21.com/a/src_j2ee_1467083655093.html

http://www.what21.com/a/src_j2ee_1466054535809.html

http://www.what21.com/a/src_java_1465880150181.html

http://www.what21.com/a/src_java_1465443667203.html

http://www.what21.com/a/search.html

http://www.what21.com/a/book_7_1475977782511.html

http://www.what21.com/a/book_4_1474767553401.html

http://www.what21.com/a/book_1_1474611495759.html

http://www.what21.com/a/src.html

http://www.what21.com/a/src_j2ee_1464583163935.html

http://www.what21.com/a/src_ui_1463211852820.html

http://www.what21.com/a/src_java_1462640583454.html

http://www.what21.com/a/src_java_1464843498044.html

http://www.what21.com/a/src_game_1462892758937.html

http://www.what21.com/a/src_java_1462643063587.html

http://www.what21.com/a/src_java_1465443667203.html

http://www.what21.com/a/src_php_1463202807211.html

http://www.what21.com/a/src_java_1463207546587.html

http://www.what21.com/a/src_java_1467351251205.html

http://www.what21.com/a/src_game_1463225237689.html

http://www.what21.com/a/src_java_1465880150181.html

http://www.what21.com/a/src_ui_1463633461191.html

http://www.what21.com/a/src_java_1464756939225.html

http://www.what21.com/a/src_j2ee_1466054535809.html

http://www.what21.com/a/book.html

http://www.what21.com/a/book_2_1462983656981.html

http://www.what21.com/a/book_5_1462769489587.html

http://www.what21.com/a/book_4_1465532916693.html

http://www.what21.com/a/book_5_1462982876975.html

http://www.what21.com/a/book_1_1462760886361.html

http://www.what21.com/a/book_7_1464438262645.html

http://www.what21.com/a/book_1_1464671869423.html

http://www.what21.com/a/book_2_1462777391221.html

http://www.what21.com/a/book_2_1465362135815.html

http://www.what21.com/a/book_3_1462871454011.html

http://www.what21.com/a/book_7_1462773332089.html

http://www.what21.com/a/book_1_1462853862836.html

http://www.what21.com/a/book_1_1462955087482.html

http://www.what21.com/a/book_4_1462865875110.html

http://www.what21.com/a/book_1_1462982195956.html

http://www.what21.com/sys/index.html

http://www.what21.com/sys/view/java_javaweb-summary_1481185974852.html

http://www.what21.com/sys/view/php_basis-php_1481184820575.html

http://www.what21.com/sys/view/javascript_2_1480647722860.html

http://www.what21.com/sys/view/php_apply_1480646222699.html

http://www.what21.com/sys/view/php_apply_1480644993514.html

http://www.what21.com/sys/view/windows_windows-summary_1479793761896.html

http://www.what21.com/sys/view/83_1_1479703138988.html

http://www.what21.com/sys/view/oracle_3_1479444258678.html

http://www.what21.com/sys/view/java_java-knowledge_1479433307860.html

http://www.what21.com/sys/view/php_summary-php_1479377252348.html

http://www.what21.com/sys/view/oracle_nosql_1479374571005.html

http://www.what21.com/sys/view/oracle_nosql_1479374105811.html

http://www.what21.com/sys/view/java_java-frame_1479364086506.html

http://www.what21.com/sys/view/java_javaweb_1479361958829.html

http://www.what21.com/sys/view/service_ws_1479360604310.html

http://www.what21.com/a/center_team.html

http://www.what21.com/a/center_contribute.html

http://www.what21.com/a/center_advert.html

http://www.what21.com/a/center_contact.html

http://www.what21.com/a/center_contact.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值