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包

也可以去这个网址下载,

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

猜你喜欢

转载自blog.csdn.net/weixin_42655593/article/details/85290213
今日推荐