Voglio estrarre il Titolo dei video di YouTube. Come posso fare questo?Ottenere il titolo dai video di YouTube
Grazie.
Voglio estrarre il Titolo dei video di YouTube. Come posso fare questo?Ottenere il titolo dai video di YouTube
Grazie.
modo più semplice per ottenere informazioni su un video di youtube è afaik per analizzare la stringa recuperato da: http://youtube.com/get_video_info?video_id=XXXXXXXX
Utilizzando qualcosa come PHP di parse_str(), è possibile ottenere una bella serie di quasi nulla della video:
$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$id);
parse_str($content, $ytarr);
echo $ytarr['title'];
Questo stamperà il titolo per il video utilizzando $ id come id del video.
Questo metodo è molto conveniente. C'è qualche documentazione ufficiale su questo metodo? Grazie. –
Non c'è quanto posso dire, il che mi porta a pensare che potrebbe non essere effettivamente il metodo best_, dal momento che get_video_info potrebbe benissimo morire proprio come ha fatto get_video. Tuttavia, utilizzando questo metodo è possibile accedere ad alcune informazioni non fornite nell'API ufficiale (come l'URL del flusso diretto per scaricare video di YouTube, come get_video fornito). Soluzione breve: se l'API YouTube ufficiale (come Alex.Bullard ha risposto qui) non fornisce le informazioni desiderate, prova a cercare get_video_info – Cruel
Grazie a @Cruel, sono solo le informazioni necessarie :) –
Con bash, wget e linci:
#!/bin/bash
read -e -p "Youtube address? " address
page=$(wget "$address" -O - 2>/dev/null)
title=$(echo "$page" | grep " - ")
title="$(lynx --dump -force-html <(echo "<html><body>
$title
</body></html>")| grep " - ")"
title="${title/* - /}"
echo "$title"
Utilizzando JavaScript API dati:
var loadInfo = function (videoId) {
var gdata = document.createElement("script");
gdata.src = "http://gdata.youtube.com/feeds/api/videos/" + videoId + "?v=2&alt=jsonc&callback=storeInfo";
var body = document.getElementsByTagName("body")[0];
body.appendChild(gdata);
};
var storeInfo = function (info) {
console.log(info.data.title);
};
Poi basta chiamare loadInfo(videoId)
.
Ulteriori informazioni sono disponibili su API documentation.
Questo non funziona più :( –
YouTube chiude le vecchie API. Dai un'occhiata qui per ulteriori informazioni sulla nuova versione https://developers.google.com/youtube/v3/getting-started. – sirLisko
// This is the youtube video URL: http://www.youtube.com/watch?v=nOHHta68DdU
$code = "nOHHta68DdU";
// Get video feed info (xml) from youtube, but only the title | http://php.net/manual/en/function.file-get-contents.php
$video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos?v=2&q=".$code."&max-results=1&fields=entry(title)&prettyprint=true");
// xml to object | http://php.net/manual/en/function.simplexml-load-string.php
$video_obj = simplexml_load_string($video_feed);
// Get the title string to a variable
$video_str = $video_obj->entry->title;
// Output
echo $video_str;
Sarà meglio aggiungere qualche commento al tuo codice –
Ok, grazie, ho aggiunto commenti ... :) – glocsw
Se si ha familiarità con java, provare il parser Jsoup.
Document document = Jsoup.connect("http://www.youtube.com/ABDCEF").get();
document.title();
Credo che il modo migliore è quello di utilizzare GData di YouTube, e poi prendere informazioni da XML restituito
http://gdata.youtube.com/feeds/api/videos/6_Ukfpsb8RI
Aggiornamento: C'è un'API più recente fuori ora che si dovrebbe usare invece
https://developers.google.com/youtube/v3/getting-started
URL: https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics
Description: This example modifies the fields parameter from example 3 so that in the API response, each video resource's snippet object only includes the channelId, title, and categoryId properties.
API response:
{
"videos": [
{
"id": "7lCDEYXw3mM",
"snippet": {
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"title": "Google I/O 101: Q&A On Using Google APIs",
"categoryId": "28"
},
"statistics": {
"viewCount": "3057",
"likeCount": "25",
"dislikeCount": "0",
"favoriteCount": "17",
"commentCount": "12"
}
}
]
}
Ecco po 'di codice taglia e incolla per ColdFusion:
http://trycf.com/gist/f296d14e456a7c925d23a1282daa0b90
Funziona su CF9 (e probabilmente, le versioni precedenti) utilizzando YouTube API v3, che richiede una chiave API.
Ho lasciato alcuni commenti e cose in diag in esso, per chiunque voglia scavare più a fondo. Spero che aiuti qualcuno.
Prova questo, sto ricevendo il nome e l'URL di ciascun video in una playlist, puoi modificare questo codice secondo le tue necessità.
$Playlist = ((Invoke-WebRequest "https://www.youtube.com/watch?v=HKkRbc6W6NA&list=PLz9M61O0WZqSUvHzPHVVC4IcqA8qe5K3r&
index=1").Links | Where {$_.class -match "playlist-video"}).href
$Fname = ((Invoke-WebRequest "https://www.youtube.com/watch?v=HKkRbc6W6NA&list=PLz9M61O0WZqSUvHzPHVVC4IcqA8qe5K3r&ind
ex=1").Links | Where {$_.class -match "playlist-video"}).outerText
$FinalText=""
For($i=0;$i -lt $playlist.Length;$i++)
{
Write-Output("'"+($Fname[$i].split("|")[0]).split("|")[0]+"'+"+"https://www.youtube.com"+$Playlist[$i])
}
Si può fare usando JSON per ottenere il video di tutte le informazioni su
$jsonURL = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id={Your_Video_ID_Here}&key={Your_API_KEY}8&part=snippet");
$json = json_decode($jsonURL);
$vtitle = $json->{'items'}[0]->{'snippet'}->{'title'};
$vdescription = $json->{'items'}[0]->{'snippet'}->{'description'};
$vvid = $json->{'items'}[0]->{'id'};
$vdate = $json->{'items'}[0]->{'snippet'}->{'publishedAt'};
$vthumb = $json->{'items'}[0]->{'snippet'}->{'thumbnails'}->{'high'}->{'url'};
spero che possa risolvere il problema.
Se script di elaborazione python batch viene apprezzato: ho usato BeautifulSoup per analizzare facilmente il titolo da HTML, urllib per scaricare le librerie HTML e unicodecsv per salvare tutti i personaggi dal titolo Youtube.
L'unica cosa che dovete fare è quello di mettere csv con colonna singola (nome) URL con URL dei video di YouTube nella stessa cartella dello script è e denominarlo YT-urls.csv ed eseguire il copione. Otterrai il file yt-urls-titles.csv contenente gli URL e i relativi titoli.
#!/usr/bin/python
from bs4 import BeautifulSoup
import urllib
import unicodecsv as csv
with open('yt-urls-titles.csv', 'wb') as f:
resultcsv = csv.DictWriter(f, delimiter=';', quotechar='"',fieldnames=['url','title'])
with open('yt-urls.csv', 'rb') as f:
inputcsv = csv.DictReader(f, delimiter=';', quotechar='"')
resultcsv.writeheader()
for row in inputcsv:
soup = BeautifulSoup(urllib.urlopen(row['url']).read(), "html.parser")
resultcsv.writerow({'url': row['url'],'title': soup.title.string})
Elaborerò il processo come indicato dallo YouTube API v3 documentation.
Creare un nuovo progetto allo https://console.developers.google.com/apis/credentials.
Creare una nuova chiave API. Ne avrai bisogno per accedere alle informazioni sul video sotto v3.
https://www.googleapis.com/youtube/v3/videos?id=<YOUR VIDEO ID HERE>&key=<YOUR API KEY HERE>%20&part=snippet
fields
e part
nell'URL sono la chiave qui.Il URL
è, beh, che cosa URL si può andare al attraverso il browser di check it out. In cambio, dovresti ottenere ciò che è sotto API response:
.
URL: https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics
Description: This example modifies the fields parameter from example 3
so that in the API response, each video resource's snippet
object only includes the channelId, title,
and categoryId properties.
API response:
{
"videos": [
{
"id": "7lCDEYXw3mM",
"snippet": {
"channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"title": "Google I/O 101: Q&A On Using Google APIs",
"categoryId": "28"
},
"statistics": {
"viewCount": "3057",
"likeCount": "25",
"dislikeCount": "0",
"favoriteCount": "17",
"commentCount": "12"
}
}
]
}
Questo vi dà il video informazioni nel formato di file .json
. Se il tuo progetto è di accedere a queste informazioni tramite JavaScript, potresti andare qui di seguito: How to get JSON from URL in Javascript?.
Estratto da dove? – Zed
ogni volta che apri il youtube, mostra un titolo sotto il titolo che il video è incorporato – webkul
accetta una risposta se risolve il tuo problema. – Reddy