【问题标题】:Jsoup retrieve youtube titleJsoup 检索 youtube 标题
【发布时间】:2018-02-19 19:30:52
【问题描述】:

我想要做的只是检索 youtube 页面的标题,到目前为止,我通过 Jsoup 完成了这项工作

title = doc.getElementById("eow-title").text();

但是现在 youtube 改变了它的布局并且该标签不再存在,我检查了 youtube html 代码,发现他们现在存储了 youtube player 标题在<script> 标记内,问题是它采用以下形式,我不知道如何检索它:

var ytplayer = ytplayer || {};ytplayer.config = {"messages":{"player_fallback":["Per la riproduzione del videoè 需要 Adob​​e Flash Player o un 浏览器 HTML5。 \u003ca href=\"https://get.adobe.com/flashplayer/\"\u003eScarica l'ultima Flash 播放器的版本 \u003c/a\u003e \u003ca href=\"/html5\"\u003eUlteriori informazioni sull'aggiornamento a un 浏览器 HTML5\u003c/a\u003e"]},"args":{"vm":"CAIQABgE","iv_invideo_url":"https://www.youtube.com/annotations_invideo?cap_hist=1\u0026video_id=wckFsik_vU8\u0026client =1\u0026ei=JY-2WfHPFIWxcpzcrKAF","watch_xlb":"https://s.ytimg.com/yts/xlbbin/watch-strings-it_IT-vflA6zD4C.xlb","pltype":"contentugc","作者":"BrawlBRSTMs3 X","title":"Big Blue - F-Zero 音乐 扩展","innertube_api_version":"v1","eventid":"JY-2WfHPFIWxcpzcrKAF",

也许我可以用一些regex 手动解析标题?我对regex 了解不多,无法解决问题,请帮忙。

附: 我已经尝试doc.getTitle(); 无济于事,我得到的只是“Youtube”而不是完整标题。

由 pleft 解决,我不得不稍微编辑代码,但这就是我让它工作的方式:

doc = Jsoup.connect(getLink()).get();
Elements script = doc.select("script");  //to get the script content
Pattern p = Pattern.compile("\"title\":\"(.+?)\""); // Regex for the getting the string: "title":"blah blah blah" 
Matcher m = p.matcher(script.html());
m.find();
title = m.group().substring(8);

【问题讨论】:

  • 仅供参考:doc.getTitle(); 获取 标签之间的文本。这显然是“YouTube”。 @pleft 的解决方案非常可靠!
  • 使用group(1)会更好,这样你就不需要substring(),引号也不会出现

标签: java html regex youtube jsoup


【解决方案1】:

是的,regex 可以解决问题。您可以尝试以下方法吗:

Element script = doc.select("script").first();  //to get the script content
Pattern p = Pattern.compile("\"title\":\"(.+?)\""); // Regex for the getting the string: "title":"blah blah blah" 
Matcher m = p.matcher(script.html());

while(m.find())
{
    System.out.println(m.group()); 
}

【讨论】:

    【解决方案2】:

    您可以尝试从源代码中读取player div,并从script 中的div 中找到title。这是使用 JSOUP 的工作示例。这将打印 "title": "Actual Title" 。所以请根据需要更新它。

      Document doc = Jsoup.connect("https://www.youtube.com/watch?v=lhs_chrfXfE").timeout(10000).get();
    
        Elements player = doc.select("div#player");
    
        for(Element e:player){
    
            Elements scriptContent = e.getElementsByTag("script");
    
            for (Element paragraph : scriptContent) {
              System.out.println(paragraph.attr("script"));
                for (DataNode node : paragraph.dataNodes()) {
                    Pattern pattern = Pattern.compile("\"title\":\"(.+?)\""); 
                    Matcher matcher = pattern.matcher(node.getWholeData());
                    if(matcher.find())
                    {
                        **//***this is your title*****
                        System.out.println(matcher.group(0));
                    }
    
                }
    
            }
    
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2013-07-18
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      相关资源
      最近更新 更多