使用 javascript/jquery 获取 Youtube 视频信息
<script type= "text/javascript">
var url = "http://gdata.youtube.com/feeds/api/videos/VA770wpLX-Q?v=2&alt=json-in-script&callback=";
var title;
var description;
var viewcount;
var views;
var author;
$.getJSON(url,
function(data){
title = data.entry.title.$t;
description = data.entry.media$group.media$description.$t;
viewcount = data.entry.yt$statistics.viewCount;
views = numberFormat (viewcount);
author = data.entry.author[0].name.$t;
listInfo (title,description,author,views);
});
</script>
这就是我从单个视频获取信息的代码,收到信息后,它会调用此函数来显示它:
<script type="text/javascript">
function listInfo (title,description,author,views) {
var html = ['<dl>'];
html.push('<dt>','<span class="titleStyle">', title,'</span><span class="descriptionStyle">',description, '</span><span class="authorStyle">',author,'</span><span class="viewsStyle">',' Views:',views,'</span></dt>');
html.push('</dl>');
document.getElementById("agenda").innerHTML = html.join("");
}
function numberFormat(nStr,prefix){
var prefix = prefix || '';
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return prefix + x1 + x2;
}
</script>
之后,它将定义列表放入一个 div 中,我将其放在一个表中,
<table width="485"><tr><td><div id="agenda"></div></td></tr></table>
所有这些信息都可以在身体,我似乎无法让它发挥作用,我已经尝试了一个星期了,但我找不到任何方法让它发挥作用
<script type= "text/javascript">
var url = "http://gdata.youtube.com/feeds/api/videos/VA770wpLX-Q?v=2&alt=json-in-script&callback=";
var title;
var description;
var viewcount;
var views;
var author;
$.getJSON(url,
function(data){
title = data.entry.title.$t;
description = data.entry.media$group.media$description.$t;
viewcount = data.entry.yt$statistics.viewCount;
views = numberFormat (viewcount);
author = data.entry.author[0].name.$t;
listInfo (title,description,author,views);
});
</script>
So thats my code to get information from a single video, after the info is received it calls this function to display it:
<script type="text/javascript">
function listInfo (title,description,author,views) {
var html = ['<dl>'];
html.push('<dt>','<span class="titleStyle">', title,'</span><span class="descriptionStyle">',description, '</span><span class="authorStyle">',author,'</span><span class="viewsStyle">',' Views:',views,'</span></dt>');
html.push('</dl>');
document.getElementById("agenda").innerHTML = html.join("");
}
function numberFormat(nStr,prefix){
var prefix = prefix || '';
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return prefix + x1 + x2;
}
</script>
After that it puts the definition List into a div, which I have inside a table
<table width="485"><tr><td><div id="agenda"></div></td></tr></table>
all of this information is found in the body, I can't seem to get it to work, I've been trying for a week now, and I can't find any way to make it work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该尝试 jTube jquery youtube 库。它使得执行这样的基本调用变得非常容易。下载/查看代码: https://github.com/monkeecreate /jTube/blob/master/jTube/jquery.jTube.js
使用方式如下:
查看更多示例:https: //github.com/defvayne23/jTube
You should try the jTube jquery youtube library. It makes it pretty easy to do basic calls like this. Download / view code at: https://github.com/monkeecreate/jTube/blob/master/jTube/jquery.jTube.js
Use like:
View more samples: https://github.com/defvayne23/jTube
虽然我不太清楚您面临的问题是什么,但为了回答您有关如何从 YouTube 视频中获取信息的问题,我在下面做了一个简单的示例。
让 youtube 视频代码在 php 中获取变量:$_GET['v']。
这将在嵌入视频(iframe)上方打印标题,在下方打印描述、视图和作者。
我不知道你还想做什么(listinfo、numberformat),但我猜你可以从这里解决。
希望这有帮助。
汤姆
While I don't really know what the problem you're facing is, to answer your question about how to get information from a youtube video, I made a quick example I below.
Having the youtube video code get variable in php: $_GET['v'].
This prints title above embedded video (iframe), and description, views and author beneath.
I don't know what more you want to do (listinfo, numberformat), but I'd guess you can work it out from here.
Hope this helps.
Tom
由于 youtube API 不允许在单个请求中返回超过 50 条评论,因此您需要添加一个名为“start-index”的 URL 参数,该参数告诉 youtube 您希望从那里开始获取评论。下面是一个例子。我已经做到了,只要响应 JSON 返回 50 条评论,它就会再次调用该函数来处理接下来的 50 条评论。
如果您还有其他问题或遇到此代码问题,请随时再次询问:-)
祝您好运,
汤姆
Since the youtube API does not allow more than 50 comments to be returned on a single request, you'll need to add a URL parameter called "start-index", which tells youtube that you want to get comments from there onwards. Below is an example. I've made it so that as long as the response JSON returns 50 comments, it calls the function again for the next 50 comments.
If you have any more questions or you get stuck with this code, don't hesitate to ask again :-)
Good luck,
Tom