PHP simple html dom parser 크롤링 관련 질문드립니다. 채택완료
api 사용하지 않고 할 수 있는 유튜브 채널페이지에서 채널이름을 가져오는 걸 알아보고 있는 크롤링이라는 재미있는게 있네요.
https://pikabu.tistory.com/124
에서 알려주는데로 아래와 같이 해보았더니, 엄청 많은 정보를 얻을 수 있었습니다.
아직 구조자체가 어려워서 그러는데, 어떤 값을 주면 채널명을 얻을 수 있을까요??
<?php
$url = "https://www.youtube.com/@syukaworld";
$str = file_get_contents_curl($url);
$html = new simple_html_dom();
$html->load($str);
foreach($html->find("text") as $data){
echo $data;
echo '<br>';
}
?>
<?php
function file_get_contents_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 웹 사이트가 https일 때 필요
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
위 페이지 실행시 출력되는 소스 중 일부 : "슈카월드" 를 뽑을 수 있으면 됩니다.
var onPolymerReady = function(e) {window.removeEventListener('script-load-dpj', onPolymerReady);if (window.ytcsi) {window.ytcsi.tick('apr', null, '');}}; if (window.Polymer && Polymer.RenderStatus) {onPolymerReady();} else {window.addEventListener('script-load-dpj', onPolymerReady);}
슈카월드 - YouTube
{"@context": "http://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "item": {"@id": "https:\/\/www.youtube.com\/channel\/UCsJ6RuBiTVWRX156FVbeaGg", "name": "슈카월드"}}]}
if (window.ytcsi) {window.ytcsi.tick('pdc', null, '');}
var ytInitialData = {"responseContext":{"serviceTrackingParams":[{"service":"GFEEDBACK","params":[{"key":"route","value":"channel."},{"key":"is_casual","value":"false"},{"key":"is_owner","value":"false"},{"key":"is_alc_surface","value":"false"},{"key":"browse_id","value":"UCsJ6RuBiTVWRX156FVbeaGg"},{"key":"browse_id_prefix","value":""},{"key":"logged_in","value":"0"},
답변 2개
foreach($html->find("text") as $data){
echo $data;
echo '<br>';
}
→
$title = $html->find('meta[property="og:title"]', 0)->getAttribute('content');
echo $title;
답변에 대한 댓글 2개
소스 원본에는 <yt-fommated-string ~> 태그는 없습니다.
에러가 난 것도, yt-formatted-string 태그를 찾지 못했다는 의미입니다.
HTML 소스 원본에서는 다음과 같은 텍스트를 찾을 수 있습니다.
"subscriberCountText":{"accessibility":{"accessibilityData":{"label":"구독자 313만명"}}
[code]
if (preg_match('/subscriberCountText.*구독자 (.*?)명/', $html->innertext, $matches))
$s_cnt = '구독자 : ' . $matches[1] . '명';
echo $s_cnt;
[/code]
댓글을 작성하려면 로그인이 필요합니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
한가지 아직 안되는게, 구독자 수 인데요, 아래와 같이 하면 에러가 떨어지네요.
어떤식으로 해야할지 궁금합니다.
<span class="meta-item style-scope ytd-c4-tabbed-header-renderer"><yt-formatted-string id="subscriber-count" class="style-scope ytd-c4-tabbed-header-renderer" aria-label="구독자 238만명">구독자 238만명</yt-formatted-string><span aria-hidden="true" class="delimiter style-scope ytd-c4-tabbed-header-renderer">‧</span></span>
//$watch = $html->find('yt-formatted-string[id="subscriber-count"]',0)->getAttribute('aria-label'); //에러떨어짐