爬取Bilibili视频评论(弹幕同样原理)
- 找到B站的评论接口
F12,找到B站的评论接口
可以看到所需要的请求头以及请求参数
请求参数:
- 可以使用Postman等,请求接口,可以看到json串(这里我直接请求,显的json串很乱)
格式化后的json串很清楚的看到结构
而我们最终需要的是message评论!当然,账号名,性别等我们也可以抓下来
- 后台java代码
maven配置:
fastJson:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
HttpClient:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0</version>
</dependency>
Java代码:
/**
* 抓评论
* @Author llb
* Date on 2019/12/3
*/
public class BilibiliGrabContent {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient();
// 设置HttpClient接收Cookie,用与浏览器一样的策略
// httpClient.getParams().setCookiePolicy("CookiePolicy.BROWSER_COMPATIBILITY");
try {
for (int i = 0; i < 4; i++) {
Thread.sleep(3000);
System.out.println("开始爬取第"+ (1+i) +"页评论。");
getMessage(httpClient, "77224508", i+1, 2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 抓数据,浏览器中找到bilibili评论接口为 https://api.bilibili.com/x/v2/reply
* 几个主要请求参数:
* type:评论类型。这里固定值 1
* oid:哪个视频
* pn:第几页的评论
* sort:排序。0:按照时间排序。2:按照热度排序。默认2
*/
public static void getMessage(HttpClient httpClient, String oid, int pn,
int sort) throws IOException {
//请求接口地址
String biliUrl = "https://api.bilibili.com/x/v2/reply?&type=1&oid="+ oid +"&pn="+ pn +"&sort="+ sort;
PostMethod postMethod = new PostMethod();
GetMethod getMethod = new GetMethod(biliUrl);
//如果网站必须要求登录才能查看评论时,这里要设置你登录后在浏览器生成的cookie值
//我这里抓别人视频评论,bilibili不用登录也可以查看
// getMethod.setRequestHeader("cookie", COOKIE);
//设置请求头
postMethod.setRequestHeader("user-agent", "postMethod.setRequestHeader(\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36");
httpClient.executeMethod(getMethod);
//获取到接口返回值
String text = getMethod.getResponseBodyAsString();
JSONObject jsonObject = JSONObject.parseObject(text);
for (int i = 0; i < 20; i++) {
String name = (String) JSONPath.eval(jsonObject, "$.data.replies["+ i +"].member.uname");
String sex = (String) JSONPath.eval(jsonObject, "$.data.replies["+ i +"].member.sex");
String message = (String) JSONPath.eval(jsonObject, "$.data.replies["+ i +"].content.message");
FileUtils.writeContent("用户:"+ name + ",性别:"+ sex + ",评论:" + message);
}
}
}
文件工具类(我这里选择将抓下来的数据保存到文件中,方便保存,查看):
/**
* 生成文件工具类
* @Author llb
* Date on 2019/12/3
*/
public class FileUtils {
public static void writeContent(String data) {
BufferedWriter bw = null;
File file = new File("content.txt");
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file.getName(), true);
bw = new BufferedWriter(fileWriter);
bw.write(data);
bw.newLine();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bw != null) {
bw.flush();
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
到此结束,很简单的找接口,分析json串,截取,保存。弹幕同样也是这个原理。
源码:github