【发布时间】: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è 需要 Adobe 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