我想做的只是检索 youtube 页面的标题,到目前为止,我通过 Jsoup 完成了此操作
title = doc.getElementById("eow-title").text();
但是现在 youtube 改变了它的布局,并且该标签不再存在,我检查了 youtube html
代码并发现他们现在将 youtube 播放器 标题存储在 <script>
中标签,问题是它采用以下形式,我不知道如何检索它:
var ytplayer = ytplayer || {};ytplayer.config = {"messages":{"player_fallback":["Per la riproduzione del video è necessario Adobe Flash Player o un browser HTML5. \u003ca href=\"https://get.adobe.com/flashplayer/\"\u003eScarica l'ultima versione di Flash Player \u003c/a\u003e \u003ca href=\"/html5\"\u003eUlteriori informazioni sull'aggiornamento a un browser 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","author":"BrawlBRSTMs3 X","title":"Big Blue - F-Zero Music Extended","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);
最佳答案
是的,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());
}
关于java - Jsoup 检索 YouTube 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157695/