├── src ├── youtube_curl.sh ├── youtube_python.py ├── youtube_nodejs.js ├── youtube_php.php ├── youtube_golang.go ├── youtube_csharp.cs └── youtube_java.java └── README.md /src/youtube_curl.sh: -------------------------------------------------------------------------------- 1 | curl --user USERNAME:PASSWORD \ 2 | 'https://realtime.oxylabs.io/v1/queries' \ 3 | -H "Content-Type: application/json" \ 4 | -d '{"source": "universal", "url": "https://www.youtube.com/watch?v=SLSGtgKWzxg", "render": "html"}' 5 | -------------------------------------------------------------------------------- /src/youtube_python.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from pprint import pprint 3 | 4 | # Structure payload. 5 | payload = { 6 | 'source': 'universal', 7 | 'url': 'https://www.youtube.com/watch?v=SLSGtgKWzxg', 8 | 'render': 'html' 9 | } 10 | 11 | # Get a response. 12 | response = requests.request( 13 | 'POST', 14 | 'https://realtime.oxylabs.io/v1/queries', 15 | auth=('USERNAME', 'PASSWORD'), # Your credentials go here 16 | json=payload 17 | ) 18 | 19 | # Instead of response with job status and results URL, this will return the 20 | # JSON response with results. 21 | pprint(response.json()) 22 | -------------------------------------------------------------------------------- /src/youtube_nodejs.js: -------------------------------------------------------------------------------- 1 | import fetch from 'node-fetch'; 2 | 3 | const username = 'USERNAME'; 4 | const password = 'PASSWORD'; 5 | const body = { 6 | 'source': 'universal', 7 | 'url': 'https://www.youtube.com/watch?v=SLSGtgKWzxg', 8 | 'render': 'html', 9 | }; 10 | const response = await fetch('https://realtime.oxylabs.io/v1/queries', { 11 | method: 'post', 12 | body: JSON.stringify(body), 13 | headers: { 14 | 'Content-Type': 'application/json', 15 | 'Authorization': 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64'), 16 | } 17 | }); 18 | 19 | console.log(await response.json()); 20 | -------------------------------------------------------------------------------- /src/youtube_php.php: -------------------------------------------------------------------------------- 1 | 'universal', 5 | 'url' => 'https://www.youtube.com/watch?v=SLSGtgKWzxg', 6 | 'render' => 'html' 7 | ); 8 | 9 | $ch = curl_init(); 10 | 11 | curl_setopt($ch, CURLOPT_URL, "https://realtime.oxylabs.io/v1/queries"); 12 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 13 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); 14 | curl_setopt($ch, CURLOPT_POST, 1); 15 | curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD"); //Your credentials go here 16 | 17 | $headers = array(); 18 | $headers[] = "Content-Type: application/json"; 19 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 20 | 21 | $result = curl_exec($ch); 22 | echo $result; 23 | 24 | if (curl_errno($ch)) { 25 | echo 'Error:' . curl_error($ch); 26 | } 27 | curl_close ($ch); 28 | ?> 29 | -------------------------------------------------------------------------------- /src/youtube_golang.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "io/ioutil" 8 | "net/http" 9 | ) 10 | 11 | func main() { 12 | const Username = "USERNAME" 13 | const Password = "PASSWORD" 14 | 15 | payload := map[string]string{ 16 | "source": "universal", 17 | "url": "https://www.youtube.com/watch?v=SLSGtgKWzxg", 18 | "render": "html", 19 | } 20 | 21 | jsonValue, _ := json.Marshal(payload) 22 | 23 | client := &http.Client{} 24 | request, _ := http.NewRequest("POST", 25 | "https://realtime.oxylabs.io/v1/queries", 26 | bytes.NewBuffer(jsonValue), 27 | ) 28 | 29 | request.SetBasicAuth(Username, Password) 30 | response, _ := client.Do(request) 31 | 32 | responseText, _ := ioutil.ReadAll(response.Body) 33 | fmt.Println(string(responseText)) 34 | } 35 | -------------------------------------------------------------------------------- /src/youtube_csharp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Net.Http; 4 | using System.Net.Http.Json; 5 | using System.Threading.Tasks; 6 | 7 | namespace OxyApi 8 | { 9 | class Program 10 | { 11 | static async Task Main() 12 | { 13 | const string Username = "USERNAME"; 14 | const string Password = "PASSWORD"; 15 | 16 | var parameters = new Dictionary<string, string>() 17 | { 18 | {"source", "universal"}, 19 | {"url", "https://www.youtube.com/watch?v=SLSGtgKWzxg"}, 20 | {"render", "html"} 21 | }; 22 | 23 | var client = new HttpClient(); 24 | 25 | Uri baseUri = new Uri("https://realtime.oxylabs.io"); 26 | client.BaseAddress = baseUri; 27 | 28 | var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/v1/queries"); 29 | requestMessage.Content = JsonContent.Create(parameters); 30 | 31 | var authenticationString = $"{Username}:{Password}"; 32 | var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.UTF8.GetBytes(authenticationString)); 33 | requestMessage.Headers.Add("Authorization", "Basic " + base64EncodedAuthenticationString); 34 | 35 | var response = await client.SendAsync(requestMessage); 36 | var contents = await response.Content.ReadAsStringAsync(); 37 | 38 | Console.WriteLine(contents); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/youtube_java.java: -------------------------------------------------------------------------------- 1 | import okhttp3.*; 2 | import org.json.JSONObject; 3 | 4 | public class Main implements Runnable { 5 | private static final String AUTHORIZATION_HEADER = "Authorization"; 6 | public static final String USERNAME = "USERNAME"; 7 | public static final String PASSWORD = "PASSWORD"; 8 | 9 | public void run() { 10 | JSONObject jsonObject = new JSONObject(); 11 | jsonObject.put("source", "universal"); 12 | jsonObject.put("url", "https://www.youtube.com/watch?v=SLSGtgKWzxg"); 13 | jsonObject.put("render", "html"); 14 | 15 | Authenticator authenticator = (route, response) -> { 16 | String credential = Credentials.basic(USERNAME, PASSWORD); 17 | 18 | return response 19 | .request() 20 | .newBuilder() 21 | .header(AUTHORIZATION_HEADER, credential) 22 | .build(); 23 | }; 24 | 25 | var client = new OkHttpClient.Builder() 26 | .authenticator(authenticator) 27 | .build(); 28 | 29 | var mediaType = MediaType.parse("application/json; charset=utf-8"); 30 | var body = RequestBody.create(jsonObject.toString(), mediaType); 31 | var request = new Request.Builder() 32 | .url("https://realtime.oxylabs.io/v1/queries") 33 | .post(body) 34 | .build(); 35 | 36 | try (var response = client.newCall(request).execute()) { 37 | assert response.body() != null; 38 | System.out.println(response.body().string()); 39 | } catch (Exception exception) { 40 | System.out.println("Error: " + exception.getMessage()); 41 | } 42 | 43 | System.exit(0); 44 | } 45 | 46 | public static void main(String[] args) { 47 | new Thread(new Main()).start(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |

3 | YouTube Scraper 4 |

5 | 6 | [![Oxylabs promo code](https://raw.githubusercontent.com/oxylabs/product-integrations/refs/heads/master/Affiliate-Universal-1090x275.png)](https://oxylabs.io/pages/gitoxy?utm_source=877&utm_medium=affiliate&groupid=877&utm_content=youtube-scraper-github&transaction_id=102f49063ab94276ae8f116d224b67) 7 | 8 | 9 | [![](https://dcbadge.limes.pink/api/server/Pds3gBmKMH?style=for-the-badge&theme=discord)](https://discord.gg/Pds3gBmKMH) [![YouTube](https://img.shields.io/badge/YouTube-Oxylabs-red?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/@oxylabs) 10 | 11 | With [YouTube 12 | Scraper](https://oxylabs.io/products/scraper-api/web/youtube), you 13 | can easily collect public YouTube data without blocks. Follow this guide 14 | to see how to scrape YouTube using Oxylabs’ [Scraper 15 | API](https://oxylabs.io/products/scraper-api). 16 | 17 | ## How it works 18 | 19 | Simply, send a web request to our API and you’ll retrieve the HTML, parsed JSSON, or Markdown output of 20 | any public YouTube page you wish to scrape. 21 | 22 | ### Python code example 23 | 24 | The below code requests and delivers the HTML content of [this 25 | Oxylabs video](https://www.youtube.com/watch?v=SLSGtgKWzxg) on 26 | YouTube: 27 | 28 | ```python 29 | import requests 30 | from pprint import pprint 31 | 32 | # Structure payload. 33 | payload = { 34 | 'source': 'universal', 35 | 'url': 'https://www.youtube.com/watch?v=SLSGtgKWzxg', 36 | 'render': 'html' 37 | } 38 | 39 | # Get a response. 40 | response = requests.request( 41 | 'POST', 42 | 'https://realtime.oxylabs.io/v1/queries', 43 | auth=('USERNAME', 'PASSWORD'), # Your credentials go here 44 | json=payload 45 | ) 46 | 47 | # Instead of response with job status and results URL, this will return the 48 | # JSON response with results. 49 | pprint(response.json()) 50 | ``` 51 | 52 | See Oxylabs 53 | [documentation](https://developers.oxylabs.io/scraper-apis/web-scraper-api) 54 | for more details. 55 | 56 | ### Output sample 57 | 58 | ```json 59 | { 60 | "results": [ 61 | { 62 | "content":"\n\n 63 | ... 64 | \n\n", 65 | "created_at": "2023-05-18 12:33:40", 66 | "updated_at": "2023-05-18 12:33:55", 67 | "page": 1, 68 | "url": "https://www.youtube.com/watch?v=SLSGtgKWzxg", 69 | "job_id": "7064941110115745793", 70 | "status_code": 200 71 | } 72 | ] 73 | } 74 | ``` 75 | 76 | Oxylabs’ YouTube Scraper API will ease your data gathering efforts and 77 | you’ll be sure to gather YouTube data like channel information, video 78 | details, titles, descriptions, playlists, and more. Reach out to us via 79 | [live chat](https://oxylabs.io/) or 80 | [email](mailto:support@oxylabs.io) in case you have any 81 | questions. 82 | 83 | 84 | Also, check this tutorial on [pypi](https://pypi.org/project/youtube-video-scraper-api/) 85 | --------------------------------------------------------------------------------