如何爬取網(wǎng)易云音樂的評(píng)論呢君仆?
1.文本以華晨宇的《我的滑板鞋2016》為例
2.使用瀏覽器的工具丐一,查找獲取評(píng)論的url
http://music.163.com/weapi/v1/resource/comments/R_SO_4_437859519/
3.不難發(fā)現(xiàn)卓练,此API是通過POST請(qǐng)求獲得我們想要的評(píng)論信息的,需要POST的參數(shù)有params和encSecKey晒哄,網(wǎng)易為了反爬蟲睁宰,加密了這2個(gè)參數(shù),不過不要緊寝凌,下面有加密過程
4.POST請(qǐng)求成功后柒傻,會(huì)得到一個(gè)JSON字符串,里面就包含了各種評(píng)論信息
5.下面就用JAVA來實(shí)現(xiàn)評(píng)論的獲取
AES加密工具類
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptTools {
//AES加密
public static String encrypt(String text, String secKey) throws Exception {
byte[] raw = secKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
// "算法/模式/補(bǔ)碼方式"
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 使用CBC模式较木,需要一個(gè)向量iv红符,可增加加密算法的強(qiáng)度
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(text.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
//字符填充
public static String zfill(String result, int n) {
if (result.length() >= n) {
result = result.substring(result.length() - n, result.length());
} else {
StringBuilder stringBuilder = new StringBuilder();
for (int i = n; i > result.length(); i--) {
stringBuilder.append("0");
}
stringBuilder.append(result);
result = stringBuilder.toString();
}
return result;
}
}
評(píng)論獲取
public void commentAPI() throws Exception {
//私鑰,隨機(jī)16位字符串(自己可改)
String secKey = "cd859f54539b24b7";
String text = "{\"username\": \"\", \"rememberLogin\": \"true\", \"password\": \"\"}";
String modulus = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
String nonce = "0CoJUm6Qyw8W8jud";
String pubKey = "010001";
//2次AES加密伐债,得到params
String params = EncryptTools.encrypt(EncryptTools.encrypt(text, nonce), secKey);
StringBuffer stringBuffer = new StringBuffer(secKey);
//逆置私鑰
secKey = stringBuffer.reverse().toString();
String hex = Hex.encodeHexString(secKey.getBytes());
BigInteger bigInteger1 = new BigInteger(hex, 16);
BigInteger bigInteger2 = new BigInteger(pubKey, 16);
BigInteger bigInteger3 = new BigInteger(modulus, 16);
//RSA加密計(jì)算
BigInteger bigInteger4 = bigInteger1.pow(bigInteger2.intValue()).remainder(bigInteger3);
String encSecKey= Hex.encodeHexString(bigInteger4.toByteArray());
//字符填充
encSecKey= EncryptTools.zfill(encSecKey, 256);
//評(píng)論獲取
Document document = Jsoup.connect("http://music.163.com/weapi/v1/resource/comments/R_SO_4_437859519/").cookie("appver", "1.5.0.75771")
.header("Referer", "http://music.163.com/").data("params", params).data("encSecKey", encSecKey)
.ignoreContentType(true).post();
System.out.println("評(píng)論:" + document.text());
}
那么如何登錄網(wǎng)易云音樂呢预侯?
1.查找登錄的API地址(在瀏覽器登錄一次,在network中查看url)
Paste_Image.png
2.查看登錄時(shí)post需要提交的參數(shù)
Paste_Image.png
3.是不是很熟悉峰锁?和剛才獲取評(píng)論的時(shí)候一樣萎馅,該如何做呢?看看下面的示例代碼
在上面AES加密工具類中添加MD5加密方法
public static String md5(String pwd) {
try {
MessageDigest digest = MessageDigest.getInstance("md5");
byte[] bs = digest.digest(pwd.getBytes());
String hexString = "";
for (byte b : bs) {
int temp = b & 255;
if (temp < 16 && temp >= 0) {
hexString = hexString + "0" + Integer.toHexString(temp);
} else {
hexString = hexString + Integer.toHexString(temp);
}
}
return hexString;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
登錄請(qǐng)求
public void loginAPI(String username, String password) throws Exception {
password = EncryptTools.md5(password);
// 私鑰虹蒋,隨機(jī)16位字符串(自己可改)
String secKey = "cd859f54539b24b7";
String text = "{\"username\": \"" + username + "\", \"rememberLogin\": \"true\", \"password\": \"" + password
+ "\"}";
String modulus = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7";
String nonce = "0CoJUm6Qyw8W8jud";
String pubKey = "010001";
// 2次AES加密糜芳,得到params
String params = EncryptTools.encrypt(EncryptTools.encrypt(text, nonce), secKey);
StringBuffer stringBuffer = new StringBuffer(secKey);
// 逆置私鑰
secKey = stringBuffer.reverse().toString();
String hex = Hex.encodeHexString(secKey.getBytes());
BigInteger bigInteger1 = new BigInteger(hex, 16);
BigInteger bigInteger2 = new BigInteger(pubKey, 16);
BigInteger bigInteger3 = new BigInteger(modulus, 16);
// RSA加密計(jì)算
BigInteger bigInteger4 = bigInteger1.pow(bigInteger2.intValue()).remainder(bigInteger3);
String encSecKey = Hex.encodeHexString(bigInteger4.toByteArray());
// 字符填充
encSecKey = EncryptTools.zfill(encSecKey, 256);
// 登錄請(qǐng)求
Document document = Jsoup.connect("http://music.163.com/weapi/login/").cookie("appver", "1.5.0.75771")
.header("Referer", "http://music.163.com/").data("params", params).data("encSecKey", encSecKey)
.ignoreContentType(true).post();
System.out.println("登錄結(jié)果:" + document.text());
}