java爬虫示例代码

package com.wpb.dao;

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;

import com.wpb.bean.Book;

public class test {

    public static void main(String[] args) throws IOException {
        //要爬取的网站
        String url = "https://www.qidian.com/search?kw=完美世界";
        //获得一个和网站的链接,注意是Jsoup的connect
        Connection connect = Jsoup.connect(url);
        //获得该网站的Document对象
        Document document = connect.get();
        int cnt = 1;
        //我们可以通过对Document对象的select方法获得具体的文本内容
        //下面的意思是获得.bool-img-text这个类下的 ul 下的 li
        Elements rootselect = document.select(".book-img-text ul li");
        for(Element ele : rootselect){
            //然后获得a标签里面具体的内容
            Elements novelname = ele.select(".book-mid-info h4 a");
            String name  = novelname.text();
            
            Elements author = ele.select(".book-mid-info p a");
            String authorname = author.first().text();
            
            Elements sumadvice = ele.select(".total p");
            String sum = sumadvice.last().text();
            
            System.out.println("书名:"+name+" 作者:"+authorname+" 推荐量:"+sum);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值