今天我们用之前第一天访问网页的方法,对我们的网页上的数据进行抽取, 即由 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="" , 那就是把匹配到的内容置空;
二、(1)Pattern p=Pattern.compile(a);
a是正则表达式
(2)Matcher 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();
}
}
}
}
结果截图: