Browser: Chromium.
Logics:
1. Follow the link to the video on Youtube;
2. After navigate to the video, new comments are loaded, until the limit is reached, and the script parses the basic data (author's nickname, comment date, comment text, number of likes);
3. All data is added to a separate file result.csv;
The script consists of:
The executable file parser_comments_youtube.php. Plug-in commands are written in the tools folder in the functions.php file.
The file result.scv records information with the results.
The log folder contains the logs of the script. There is a separate log file for each day.
Download script: Script settings:
//link to video $url = "youtube.com/watch?v=moOxq_8l_34"; $comments_limit = 20;
Script code:
//follow the link to the video $browser->navigate($url); sleep(4); //scroll through the scroll until the required number of comments is loaded while(true) { // get the given objects $obj = $element->get_all_by_tag("YTD-COMMENT-THREAD-RENDERER"); // count $cur_num_obj = count($obj->elements); echo "comments: $cur_num_obj <br>"; if($cur_num_obj < $comments_limit) { $browser->set_vertical_scroll_pos($browser->get_page_height() + 100); sleep(3); } else { break; } } $result = ""; foreach($obj->elements as $item) { //go down into nested tags until we find the one we need $it = $item->get_child_by_attribute("class", "style-scope ytd-comment-renderer", true, true); $text = $it->get_inner_text(); $a = explode("\n", $text); $name = "Name:" . $a[0]; $date = "Date:" . $a[1]; //number of likes $cnt = count($a); $votes = "Number of likes:" . $a[$cnt-2]; //collect comments $comment = "Comment:" . $a[2]; for($j = 3; $j < $cnt-2; $j++) { if($a[$j] == "Read more") { break; } $comment .= "\n" . $a[$j]; } $result = $result . $name . "\n" . $date . "\n" . $comment . "\n" . $votes . "\n" . "\n"; } file_put_contents("result.csv", $result);
Example of a file with the result of the script:
Name:QueenYoko OfKei Date:1 день назад Comment:You know Trump was imagining the golf balls were Biden’s face. Number of likes:2,5 тыс. Name:Zach Coates Date:10 часов назад Comment:“How you get fired on your day off???” Number of likes:2 тыс. Name:Nite & Day Date:11 часов назад Comment:Ended how it started. On a Golf Course of course. Number of likes:1,3 тыс.