- 首先创建一个maven项目,并导入Jsoup包`
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
</dependencies>
- 去B站找到你想要爬弹幕的视频页面
java爬虫爬取B站弹幕
右键点击查看网页源代码
按Ctrl + F输入cid 搜索
找到并复制cid的值
http://comment.bilibili.com/20746041.xml
将你获取的cid替换.com后面的数字,然后这个xml文件就是该视频的所有弹幕
- 接下来就是JAVA代码了
public class Demo {
public static void main(String args[]) throws IOException {
//获取整个页面
Document document = Jsoup.connect("http://comment.bilibili.com/20746041.xml").get();
//获取所有的d标签 也就是存放弹幕的标签
Elements d = document.getElementsByTag("d");
//获取File
File file = new File("D://卢本伟.txt");
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fileOutputStream=new FileOutputStream(file);
//遍历所有的d标签
for(Element element: d){
//获取每条弹幕换行并添加到txt文件中
fileOutputStream.write((element.text()+"\r\n").getBytes());
}
fileOutputStream.close();
}
}
是不是很简单
右键运行,然后打开路径对应的文件就像下面这样,就获取到整个视频的弹幕啦