(一)实现思路
1,定位弹幕文件
一般用json或xml格式来保存弹幕,所以我们只要找到视频网页里面的xml文件或json文件,就能定位到弹幕文件。
2,解析弹幕文件
然后通过jsoup解析文件,提取我们弹幕的文本内容。
(二)第一个实现方案,解析本地文件
1,定位弹幕文件
比如我们希望爬取下方视频的弹幕文件。

打开Chrome的Network后刷新网页,再输入框中输入xml筛选出xml文件资源:
光标移动到该文件上,可以看到该文件具体地址如下:
在该文件上右键Open in new tab就可以在新的浏览器页面查看该弹幕文件内容:
2,解析弹幕文件
2.1 创建基本的maven项目
输入GroupId和ArtifactId
本项目中会使用到jsoup这个jar包,于是在项目根目录下创建lib目标,把jar拷贝进去,然后按下面操作将jar包构建到项目中:
选中该jar并点击OK。
2.2 在项目根目录下创建弹幕文件
在根目录下创建3232417.xml文件,复制https://comment.bilibili.com/3232417.xml弹幕页面的内容,保存到该文件中。直接全选复制过去即可,我们后面解析文件的时候只会提取有用的文本,所以第一行内容不用去除,如下:
2.3 代码实现
解析本地弹幕xml文件的代码如下:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.util.ArrayList;
public class LocalFile {
public static ArrayList getData(String fileName){
ArrayList list = new ArrayList();
try{
File input = new File(fileName);
Document doc = Jsoup.parse(input, "UTF-8");
//每条弹幕的内容都处于标签中,于是根据该标签找到所有弹幕
Elements contents = doc.getElementsByTag("d");
for (Element content : contents) {
list.add(content.text()); //将标签中的文本内容,也就是弹幕内容,添加到list中
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
复制代码
在入口类Main.java中调用LocalFile类的getData方法,传入参数为xml文件名,解析每条弹幕并输出:
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
ArrayList items = new ArrayList();
//1,通过解析本地文件的方式得到所有弹幕
items = LocalFile.getData("3232417.xml");
//遍历输出每条弹幕
for (String item : items) {
System.out.println(item);
}
}
}
复制代码
输出结果如下:
(三)第二个实现方案,解析远程服务器文件
1,添加httpclient依赖
由于需要访问远程服务器,所以用到了相关的依赖,该依赖提供了对http服务器的访问功能。在pom.xml文件中添加:
org.apache.httpcomponents
httpclient
4.3.3
复制代码
2,实现代码
import org.apache.http.HttpEntity;
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 org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
public class RemoteFile {
public static ArrayList getData(String fileName) throws IOException {
ArrayList list = new ArrayList();
//1,创建HttpClient对象,我们使用到Apache中的HttpClient的实例CloseableHttpClient
CloseableHttpClient httpclient = HttpClients.createDefault();
//2,创建HttpGet请求实例,该实例指示向目标URL发起GET请求
HttpGet httpGet = new HttpGet(fileName);
//3,执行HttpGet请求实例,也就是发起GET请求,响应结果保存到httpResponse变量中
CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
try {
//4,得到弹幕文件的文件内容
HttpEntity httpEntity = httpResponse.getEntity();
String httpHtml = EntityUtils.toString(httpEntity);
//5,解析弹幕文件,把每条弹幕放入list中
Document doc = Jsoup.parse(httpHtml, "UTF-8");
Elements contents = doc.getElementsByTag("d");
for (Element content : contents) {
list.add(content.text()); //将标签中的文本内容,也就是弹幕内容,添加到list中
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpResponse.close();
}
return list;
}
}
复制代码CloseableHttpResponse httpResponse = httpclient.execute(httpGet);执行完GET请求后,响应结果存放在httpResponse中。response.getEntity()是响应结果中的消息实体,因为响应结果中还包含其他内容比如Headers等如下图,这里我们只需要关注getEntity()消息实体即可。
EntityUtils.toString(response.getEntity());返回的是服务端以流的形式写出的响应内容,比如在服务端调用的方法最后为:responseWriter.write("just do it");那么EntityUtils.toString(response.getEntity());获取的就是just do it 这句话。
这里可以简单理解为网页的html代码,即右键查看网页源代码看到的全部html代码。我们需要解析的就是这样的html代码。
在入口类Main.java中调用RemoteFile类的getData方法,传入参数为xml文件名,解析每条弹幕并输出:
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
ArrayList items = new ArrayList();
//1,通过解析本地文件的方式得到所有弹幕
// items = LocalFile.getData("3232417.xml");
//2,通过解析远程服务器文件的方式得到所有弹幕
items = RemoteFile.getData("https://comment.bilibili.com/3232417.xml");
//遍历输出每条弹幕
for (String item : items) {
System.out.println(item);
}
}
}
复制代码
输出结果如下:
项目代码