使用JSoup实现爬虫操作(爬取网页图片、文章内容)

一、基础配置:

-<dependency>

<groupId>org.jsoup</groupId>

<artifactId>jsoup</artifactId>

<version>1.12.1</version>

</dependency>

二、代码程序:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
//我们要读取网站中的前100篇
//分页的时候,每一页有18篇
//100篇就是(18*5+10)
public class Reptile03 {
    public static void main(String[] args) {
        int count=0;  //计数器
        int totalQuantity=100;//总共100篇
        int page=0;  //从第一页开始  在这个题目中  page最大就是6   第六页的第十篇

        ReptileService reptileService = new ReptileService();
        String url = "https://www.qlu.edu.cn/gdyw/list1.htm"; //初始页面
        Document document=null;
        Elements elements=null;

        while(count<100){
            try {
                document =  Jsoup.connect(url).get();
                 elements = document.select("div.col_news_con>div>div>ul>li>span>a");
//                System.out.println(elements.size());  //可以查看有多少条内容

                for(Element element:elements){
                    count++;// 每读取一个数据,计数器就+1
                    if(count==totalQuantity+1){
                        System.exit(0);
                    }


//             文章链接        System.out.println(element.attr("href"));
                    System.out.println("****************************************************************************************************************************************************************************************************");//分割线
                    System.out.println("****************************************************************************************************************************************************************************************************");//分割线
                    System.out.println("文章章节数:"+count);             //篇数
                    System.out.println(element.ownText());//文章标题

//                通过文章链接提取文章内容
//                先判断文件是否是https开始
                    if(element.attr("href").startsWith("https")){
                        System.out.println("文章链接:"+element.attr("href"));
//                    运行到这里,说明是https开始,我们直接从连接中提取

                        reptileService.getArticleByOfficial(element.attr("href"));
                    }else{
                        System.out.println("文章链接:"+"https://www.qlu.edu.cn"+element.attr("href"));
//                    运行到这里,说明不是https开始,我们要改变一下
                        reptileService.getArticle("https://www.qlu.edu.cn"+element.attr("href"));
                    }
                }

               url= reptileService.changePage(url,page,count);
                System.out.println(url);


            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class ReptileService {



//    从href中获取文章的内容
    public void getArticle(String  href){
        Elements articles =null;
        Elements articlesPicture;
        try {
            Document document =  Jsoup.connect(href).get();
            //文字
            articles = document.select("div.wp_articlecontent>p");

            for(Element article:articles){

//                获取文章文字
                System.out.println(article.ownText());//但是数据比较冗杂
            }
//            图片
            articlesPicture = document.select("div.wp_articlecontent>p>img");

            for(Element picture:articlesPicture){
                System.out.println("https://www.qlu.edu.cn/"+picture.attr("src"));//但是数据比较冗杂
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//    从href中获取文章的内容(来源于公众号)
    public void getArticleByOfficial(String  href){
        Elements articles =null;
        Elements articlesPicture=null;
        try {
            Document document =  Jsoup.connect(href).get();
//            文字
            articles = document.select("div.rich_media_content>section>p>span");

            for(Element article:articles){
                System.out.println(article.ownText());//但是数据比较冗杂
            }
            //           图片
            articlesPicture = document.select("div.rich_media_content>section>p>img");

            for(Element picture:articlesPicture){
                System.out.println(picture.attr("src"));//但是数据比较冗杂
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    当此页面的内容提取完了之后,要换下一个分页
    public String changePage(String url,int page,int count){
        page = (count/18)+1;   //比如count是22时,page就是2,刚好就是第二页
        System.out.println("下一次进入页数:"+page+",count:"+count);
        url = url.replace(url.substring(28,33),"list"+String.valueOf(page)); //当页数改变的时候,url也需要改变
        return url;

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱布朗熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值