Last active
June 29, 2020 05:42
-
-
Save nick-hoang/ed4282edd6e22d8283462e373f9504a9 to your computer and use it in GitHub Desktop.
Use selenium to get the paging token, do post to youtube and parse the video list from response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var driver = new FirefoxDriver()) | |
{ | |
driver.Url = "https://www.youtube.com/results?search_query=m%E1%BB%B9+t%C3%A2m"; | |
var wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30)) | |
{ | |
PollingInterval = TimeSpan.FromSeconds(5), | |
}; | |
wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); | |
var content = wait.Until(drv => drv.FindElement(By.TagName("ytd-video-renderer"))); | |
IJavaScriptExecutor js = driver as IJavaScriptExecutor; | |
var continuationToken = (string)js.ExecuteScript("return window.ytInitialData.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.continuations[0].nextContinuationData.continuation"); | |
var clickTrackingParams = (string)js.ExecuteScript("return window.ytInitialData.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.continuations[0].nextContinuationData.clickTrackingParams"); | |
//Prepare headers | |
var uagent = js.ExecuteScript("return navigator.userAgent;"); | |
var cookies = driver.Manage().Cookies; | |
var INNERTUBE_CONTEXT_CLIENT_VERSION = (string)js.ExecuteScript("return ytcfg.data_.INNERTUBE_CONTEXT_CLIENT_VERSION;"); | |
var httpClient = new HttpClient(); | |
httpClient.DefaultRequestHeaders.Add("X-YouTube-Client-Name", "1"); | |
httpClient.DefaultRequestHeaders.Add("X-YouTube-Client-Version", INNERTUBE_CONTEXT_CLIENT_VERSION); | |
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"); | |
var url = $"https://www.youtube.com/results?search_query=m%E1%BB%B9+t%C3%A2m&pbj=1&ctoken={continuationToken}&continuation={continuationToken}&itct={clickTrackingParams}"; | |
var response = httpClient.GetAsync(url).Result; | |
response.EnsureSuccessStatusCode(); | |
string result = response.Content.ReadAsStringAsync().Result; | |
//Parse the video list from result | |
Console.WriteLine(result); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment