在 Java 中获取没有任何 API 的 youtube 视频标签
Get youtube video tags without any API in Java
我一直在尝试从 youtube 视频 ID 获取 youtube 视频标签,到处搜索但在 PHP 中得到了答案,我没有尝试任何东西,因为我是新手 java 开发者。
这是一个漫长而复杂的过程,但我会尽力解释如何做到这一点。
- 为视频创建一个 url 对象
URL url = new URL(video url goes here!);
- 打开一个url连接
URLConnection urlCon = url.openConnection();
- 从连接读取
InputStream inputStream = urlCon.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
String line = "";
while((line = br.readLine())!=null){
//now we parse the information here!
}
- 这是最难的部分...我们需要解析 reader...
我无法为您完成这部分,因为它需要大量时间和代码,但这是最快捷的方法:转到 url 并打开页面的源代码 (ctr + u),查看源,直到找到带有标签的区域。现在您必须一遍又一遍地解析字符串,直到获得所需的输出。
在浏览了一个 youtube 页面的源代码后,我注意到 youtube 标签存储为 keywords
。
这里看起来像 "keywords":["James","Gosling","Sun","Microsystems","Students"]
我们只需要提取一个以keywords
开头并以]
结尾的字符串,然后清洗数据
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class YoutubeTags
{
public static void main(String[] args) {
final String baseUrl = "https://www.youtube.com/watch?v=";
String videoId = "r19P3y1VBiw";
String tags[] = null;
try {
URL url = new URL(baseUrl + videoId);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
Pattern pattern = Pattern.compile("\"keywords([^\]])*");
String line;
while ((line = br.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
tags = matcher.group().split(",");
break;
}
}
} catch (Exception e) {
// do something
}
// cleaning data
for(int i = 0; i < tags.length; i++) {
tags[i] = tags[i].substring(2, tags[i].length() - 2);
if(i == 0) {
tags[0] = tags[0].substring(tags[0].lastIndexOf("\"") + 1);
}
}
System.out.println(Arrays.toString(tags));
}
}
输出
[James, Gosling, Sun, Microsystems, Students]
注意:我使用这个 post 作为我回答的基础。为了便于理解,我删除了一些代码。
我一直在尝试从 youtube 视频 ID 获取 youtube 视频标签,到处搜索但在 PHP 中得到了答案,我没有尝试任何东西,因为我是新手 java 开发者。
这是一个漫长而复杂的过程,但我会尽力解释如何做到这一点。
- 为视频创建一个 url 对象
URL url = new URL(video url goes here!);
- 打开一个url连接
URLConnection urlCon = url.openConnection();
- 从连接读取
InputStream inputStream = urlCon.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
String line = "";
while((line = br.readLine())!=null){
//now we parse the information here!
}
- 这是最难的部分...我们需要解析 reader... 我无法为您完成这部分,因为它需要大量时间和代码,但这是最快捷的方法:转到 url 并打开页面的源代码 (ctr + u),查看源,直到找到带有标签的区域。现在您必须一遍又一遍地解析字符串,直到获得所需的输出。
在浏览了一个 youtube 页面的源代码后,我注意到 youtube 标签存储为 keywords
。
这里看起来像 "keywords":["James","Gosling","Sun","Microsystems","Students"]
我们只需要提取一个以keywords
开头并以]
结尾的字符串,然后清洗数据
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class YoutubeTags
{
public static void main(String[] args) {
final String baseUrl = "https://www.youtube.com/watch?v=";
String videoId = "r19P3y1VBiw";
String tags[] = null;
try {
URL url = new URL(baseUrl + videoId);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
Pattern pattern = Pattern.compile("\"keywords([^\]])*");
String line;
while ((line = br.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
tags = matcher.group().split(",");
break;
}
}
} catch (Exception e) {
// do something
}
// cleaning data
for(int i = 0; i < tags.length; i++) {
tags[i] = tags[i].substring(2, tags[i].length() - 2);
if(i == 0) {
tags[0] = tags[0].substring(tags[0].lastIndexOf("\"") + 1);
}
}
System.out.println(Arrays.toString(tags));
}
}
输出
[James, Gosling, Sun, Microsystems, Students]
注意:我使用这个 post 作为我回答的基础。为了便于理解,我删除了一些代码。