写一个java的爬取视频的代码
时间: 2024-05-02 11:19:38 浏览: 8
可以的,以下是一个简单的Java爬虫爬取视频的代码示例:
```
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class VideoSpider {
private static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36";
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入视频网站URL:");
String url = scanner.next();
Document doc = Jsoup.connect(url).userAgent(USER_AGENT).get();
String title = doc.select("title").text();
System.out.println("正在下载视频:" + title);
System.out.print("请输入保存路径:");
String folderPath = scanner.next();
Elements sources = doc.select("source");
for (Element source : sources) {
String src = source.attr("src");
String type = source.attr("type");
if (type.contains("video")) {
String filename = extractFilename(src);
String filepath = folderPath + File.separator + filename;
downloadVideo(src, filepath);
}
}
System.out.println("下载完成!");
}
private static void downloadVideo(String url, String savePath) throws IOException {
URL videoUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) videoUrl.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", USER_AGENT);
InputStream inputStream = conn.getInputStream();
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
}
private static String extractFilename(String url) {
int lastSlashIndex = url.lastIndexOf("/");
if (lastSlashIndex >= 0 && lastSlashIndex < url.length() - 1) {
return url.substring(lastSlashIndex + 1);
}
return "";
}
}
```
请注意,该代码仅供学习和研究使用,请勿用于非法用途。