英文:
Get youtube video tags without any API in Java
问题
我一直在尝试从YouTube视频ID获取视频标签,在各个地方进行了搜索,但得到的是PHP代码的答案,由于我是一个新手Java开发者,我还没有尝试任何操作。
英文:
I have been trying to get youtube video tags from youtube video ID, searched everywhere but got answer in PHP, I did not try anything because I am a newbie java developer.
答案1
得分: 0
这是一个又长又复杂的过程,但我会尝试解释如何做到这一点。
- 创建一个视频的URL对象:
URL url = new URL(video的URL放在这里!);
- 打开一个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) {
// 现在我们在这里解析信息!
}
- 这是最困难的部分... 我们需要从阅读器中解析信息...
我不能为您完成这部分,因为它需要大量的时间和代码,但这是最快的方法:转到该URL,打开页面的源代码(Ctrl + U),在源代码中查找带有标签的区域。现在,您必须一遍又一遍地解析字符串,直到获得所需的输出。
英文:
This is a long and complicated process but I will try to explain how to do this.
- create a url object to the video
URL url = new URL(video url goes here!);
- open a urlconnection
URLConnection urlCon = url.openConnection();
- read from the connection
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!
}
- this is the hardest part... We need to parse the information from the reader...
I can not do this part for you because it takes a lot of time and code, but this is the fatest way to do it: go to that url and open the page's source (ctr + u), look through the source until you find the area with the tags. Now you have to parse strings over and over until you get the desired output.
答案2
得分: 0
以下是您要翻译的内容:
"After going through the source code of a youtube page, I noticed youtube tags are stored as keywords
."
"here how it looks like "keywords":["James","Gosling","Sun","Microsystems","Students"]
"
"We just need to extract a string that starts with keywords
and ends with ]
which is followed by cleaning data"
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));
}
}
output
[James, Gosling, Sun, Microsystems, Students]
Note: I used this post as the base for my answer. I have removed some code for easy understanding.
英文:
After going through the source code of a youtube page, I noticed youtube tags are stored as keywords
.
here how it looks like "keywords":["James","Gosling","Sun","Microsystems","Students"]
We just need to extract a string that starts with keywords
and ends with ]
which is followed by cleaning data
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));
}
}
output
[James, Gosling, Sun, Microsystems, Students]
Note: I used this post as the base for my answer. I have removed some code for easy understanding.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论