获取YouTube视频的标签,无需使用任何API,使用Java实现。

huangapple go评论67阅读模式
英文:

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

这是一个又长又复杂的过程,但我会尝试解释如何做到这一点。

  1. 创建一个视频的URL对象:URL url = new URL(video的URL放在这里!);
  2. 打开一个URL连接:URLConnection urlCon = url.openConnection();
  3. 从连接中读取数据:
InputStream inputStream = urlCon.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
String line = "";
while ((line = br.readLine()) != null) {
    // 现在我们在这里解析信息!
}
  1. 这是最困难的部分... 我们需要从阅读器中解析信息...
    我不能为您完成这部分,因为它需要大量的时间和代码,但这是最快的方法:转到该URL,打开页面的源代码(Ctrl + U),在源代码中查找带有标签的区域。现在,您必须一遍又一遍地解析字符串,直到获得所需的输出。
英文:

This is a long and complicated process but I will try to explain how to do this.

  1. create a url object to the video URL url = new URL(video url goes here!);
  2. open a urlconnection URLConnection urlCon = url.openConnection();
  3. 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!
}
  1. 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 &quot;keywords&quot;:[&quot;James&quot;,&quot;Gosling&quot;,&quot;Sun&quot;,&quot;Microsystems&quot;,&quot;Students&quot;]

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 = &quot;https://www.youtube.com/watch?v=&quot;;
        String videoId = &quot;r19P3y1VBiw&quot;;
        
        String tags[] = null;
        try {
            URL url = new URL(baseUrl + videoId);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            
            Pattern pattern = Pattern.compile(&quot;\&quot;keywords([^\\]])*&quot;);
            String line;
            while ((line = br.readLine()) != null) {
                Matcher matcher = pattern.matcher(line);
                if (matcher.find()) {
                    tags = matcher.group().split(&quot;,&quot;);
                    break;
                }
            }
        } catch (Exception e) {
            // do something
        }

        // cleaning data
        for(int i = 0; i &lt; tags.length; i++) {
            tags[i] = tags[i].substring(2, tags[i].length() - 2);
            if(i == 0) {
                tags[0] = tags[0].substring(tags[0].lastIndexOf(&quot;\&quot;&quot;) + 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.

huangapple
  • 本文由 发表于 2020年9月20日 22:29:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63980022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定