Jsoup抓取到页面A标签中的href路径

博客地址:博客地址

直接上代码,注释很全乎

    public static void main(String[] args)throws Exception{
        //抓取的网址
        String url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2017/index.html";
        //编码格式的转换
        Document document = Jsoup.parse(new URL(url).openStream(), "GBK", url);
        //根据class获取到 页面的 元素内容
        Elements tables = document.getElementsByClass("provincetr");
        //根据td标签来划分
        Elements td = tables.select("td");
        for(int j=0;j<td.size();j++){
            //获取到标签中的内容
            String text = td.get(j).text();
            System.out.println(text);
            //获取A标签的href 网址  select 获取到当前A标签 attr href 获取到地址
            String s = td.get(j).select("a").attr("href");
            System.out.println(s);
        }
    }
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>

需要引入以上jar包

也可以去这个网址下载,

输出的时候会抓到  ""  空字符串,大家自己判断下就好了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值