Get title from YouTube videos

前端 未结 17 1823
抹茶落季
抹茶落季 2020-12-07 17:30

I want to extract the Title of YouTube\'s videos. How can I do this?

Thanks.

相关标签:
17条回答
  • 2020-12-07 17:37

    One way to do this would be to retrieve the video from youtube as shown here

    Then extract the title out of the atom feed sent by youtube. A sample feed is shown here

    0 讨论(0)
  • 2020-12-07 17:37

    With bash, wget and lynx:

    #!/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"
    
    0 讨论(0)
  • 2020-12-07 17:37

    Try this, I am getting name and url of each video in a playlist, you can modify this code as per your requirement.

    $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])
    }
    
    0 讨论(0)
  • 2020-12-07 17:38

    Easiest way to obtain information about a youtube video afaik is to parse the string retrieved from: http://youtube.com/get_video_info?video_id=XXXXXXXX

    Using something like PHP's parse_str(), you can obtain a nice array of nearly anything about the video:

    $content = file_get_contents("http://youtube.com/get_video_info?video_id=".$id);
    parse_str($content, $ytarr);
    echo $ytarr['title'];
    

    That will print the title for the video using $id as the video's id.

    0 讨论(0)
  • 2020-12-07 17:38

    I'll lay out the process as outlined by the YouTube API v3 documentation.

    1. Make a / login to the Google account that you want to be associated with your YouTube API use.
    2. Create a new project at https://console.developers.google.com/apis/credentials.

      • On the upper left, next to the Google APIs logo, go to Select a project and Create project +.
      • Wait a moment for the creation to finish.
    3. Make a new API key. You'll need it to access video info under v3.

      • If you're not already there, go to Credentials under the navigator on the left hand side, APIs and Services > Credentials.
      • Under the Credentials tab, click Create Credentials and select API key.
      • Copy the API key to your clipboard.
    4. Providing a video ID and your newly created API key, go to this link to see your work in action: https://www.googleapis.com/youtube/v3/videos?id=<YOUR VIDEO ID HERE>&key=<YOUR API KEY HERE>%20&part=snippet (no angle brackets)
      • For more info on what you can access, see here: https://developers.google.com/youtube/v3/getting-started#partial. For convenience, I'll copy one of their examples here (Example 4). The fields and part parameters in the URL are key here.

    Example

    The URL is, well, what URL you can go to through your browser to check it out. In return, you should get what's under 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"
       }
      }
     ]
    }
    

    This gives you video info in the .json file format. If your project is to access this info through JavaScript, you may be going here next: How to get JSON from URL in Javascript?.

    0 讨论(0)
  • 2020-12-07 17:39

    If you are familiar with java, try the Jsoup parser.

    Document document = Jsoup.connect("http://www.youtube.com/ABDCEF").get();
    document.title();
    
    0 讨论(0)
提交回复
热议问题