Java爬虫学习 第三天

本文介绍了使用Java爬虫进行网页数据抽取的过程,通过httpClient获取网页内容后,利用正则表达式从字符串中提取所需信息。以起点中文网的不科学御兽小说正文为例,详细阐述了从创建SpringBoot项目、引入依赖、网页分析到编写成功代码的步骤。
摘要由CSDN通过智能技术生成

今天我们用之前第一天访问网页的方法,对我们的网页上的数据进行抽取, 即由 httpClient 提取出来网页之后, 因为结果是字符串的形式,所以我们需要一种特殊的语言或者一个好的工具为我们从”杂乱无章的字符串“ 中提取出我们需要的信息, 这个时候, 正则表达式就派上用场了。
那么今天的实验开始 :
第一步:
找到我们今天的目标页
https://read.qidian.com/chapter/s8jssYMfwMbhI-Ha6N4TBg2/9phQvALALFm2uJcMpdsVgA2/
起点中文网的不科学御兽小说正文的爬取
在这里插入图片描述

第二步 ,
在springboot 上创建好项目, 并且引入相关场景的依赖

  <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>
        <dependency>
         <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

第三步:
对网页进行分析, 找到对应的组件,
在这里插入图片描述

第四步:
我们跟前面一样先对整体的数据进行抽取, 形成一个整体的字符串。 然后通过:

一、 String.replaceAll(a, b);
a 是正则表达式 , b是要换成什么, 如果b="" , 那就是把匹配到的内容置空;
二、(1Pattern p=Pattern.compile(a);
a是正则表达式
   (2Matcher m=p.matcher(content);
content是抽取出来的字符串
这两句话的意思是提取出所有匹配正则表达式的字符串
如果m.find()true就代表有匹配的
此时 m.group()返回的字符串就是我们所需要的
三、 就是我发现以正则表达式从字符
串中抽取出我们想要的信息有时候不能一步就位
要通过多次的抽取

第五步
成功的代码如下:

package com.example.demo;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test3 {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient= HttpClients.createDefault();
        HttpGet httpGet=new HttpGet("https://read.qidian.com/chapter/s8jssYMfwMbhI-Ha6N4TBg2/9phQvALALFm2uJcMpdsVgA2/");
        CloseableHttpResponse response=null;
        try
        {
             response=httpClient.execute(httpGet);
             if(response.getStatusLine().getStatusCode()==200) {

                 String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                 System.out.println(content);

//                  Pattern p=Pattern.compile("\t|\r|\n");
//                  Matcher m=p.matcher(content);
                 content = content.replaceAll("\t|\r|\n", "");
                 //#j_659280323 > p:nth-child(1) > span.content-wrap
                 //<div class="read-content j_readContent" id="j_659280323">

                 Pattern p = Pattern.compile("<div class=\"read-content j_readContent\" id=\"j_659280323\">.*?(</div>)");
                 Matcher m = p.matcher(content);
                 System.out.println();
                 System.out.println();
                 System.out.println("dasdasda");
                 while (m.find()) {

                     content = m.group();
                     content = content.replaceAll("<div class=\"read-content j_readContent\" id=\"j_659280323\">", "");
                     content = content.replaceAll("</div>", "");
                     content = content.replaceAll("\\s", "");

                     String[] ins = content.split("<p>");
                     for (String i : ins) {
                         System.out.println(i);
                     }

                 }
             }
             else
             {
                 System.out.println(response.getStatusLine().getStatusCode());
             }
        }
        catch(Exception e)
        {
             e.printStackTrace();
        }
        finally
        {
             if(response!=null )
             {
                 try
                 {
                      response.close();
                 }
                 catch(IOException e)
                 {
                      e.printStackTrace();
                 }
                 httpClient.close();
             }
        }

    }
}

结果截图:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

絨辉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值