|
【撸哥干货】Java爬取抖音分享页无水印视频地址[复制链接]
复制代码
1.创建一个类2.把代码贴进去
3.更改包名
4.导入依赖(不会maven的去下载jsoup的jar包)
5.去main方法调用即可
在学习Java的小伙伴可以加我交流哦QQ9837204
package cn.why0.util;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Dy {
public static String jiexi(String url){
try {
if (url == null || "".equals(url)) {
return "400";
}
//1.利用Jsoup抓取抖音链接
String htmls = Jsoup.connect(url).ignoreContentType(true).execute().body(); //抓取抖音网页
//2.利用正则匹配可以抖音下载链接
//playAddr: "https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200ffc0000bfil6o4mavffbmroeo80&line=0",
//具体匹配内容格式:「https://aweme.snssdk.com/aweme/...line=0」
Pattern patternCompile = Pattern.compile("(?<=playAddr: \")https?://.+(?=\",)");
Matcher m = patternCompile.matcher(htmls);
String matchUrl = "";
while (m.find()) {
matchUrl = m.group(0).replaceAll("playwm", "play");
}
Map<String, String> headers = new HashMap<>();
headers.put("Connection", "keep-alive");
headers.put("Host", "aweme.snssdk.com");
headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1");
BufferedInputStream in = Jsoup.connect(matchUrl).headers(headers).timeout(10000).ignoreContentType(true).execute().bodyStream();
Connection con = Jsoup.connect(matchUrl).ignoreContentType(true).followRedirects(false);
con.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1");
Connection.Response resp=con.method(Connection.Method.GET).execute();
Map<String,String> headersOne =resp.headers();
// Document doc = Jsoup.connect(matchUrl).ignoreContentType(true).followRedirects(true).execute().parse();
return headersOne.get("location");
}catch (Exception e){
e.printStackTrace();
return "erro";
}
}
}
|
|