플레이스 정보 파싱 (simple_html_dom)
본문
simple_html_dom.php 내 file_get_html 함수를 이용해서
상호명을 가져오려합니다.
<meta id="og:title" property="og:title" content="우리정형외과의원 : 네이버" data-isomorphic-meta="true"/>
원하는 결과 --> 우리정형외과의원 추출!!
target_url이 블로그주소로 했을경우 블로그 제목을 og태그안에서 추출할 수 있었습니다.
플레이스주소의 경우 file_get_html 자체가 동작을 안하는데, 플레이스내 og태그 title 만 추출하는건데도
막혀있는건지요?
이전에 비슷한 글을올린적 있습니다. 그때는 파이썬으로 동적인 파싱을 하라는 조언을 받았습니다.
하지만 og태그내 title만 추출하는거라서, 읽어들이기만 하면 가능할법 하여
다시한번 글 남겨봅니다.
<?
ini_set("allow_url_fopen","1");
include('./simple_html_dom.php');
$target_url = "https://m.place.naver.com/hospital/37878766/home";
$html = file_get_html($target_url);
if($html){
echo "a";
foreach($html->find('meta[property=og:title]') as $article) {
$sample = explode("<meta property=\"og:title\" content=\"" ,$article);
$temp = explode("\"/>" , $sample[1] );
$site_subject = iconv('UTF-8','EUC-KR',$temp[0]);
} //foreach
} //$html
echo "==>".$site_subject ;
echo "a"; 를 출력해봤는데, 아예 찍히지 않습니다.
읽어들이질 못하네요.
혹시 동적으로 생성되는 페이지라 file_get_html 로는 불가한가요?
간단하게 상호명만 추출할수 있는 팁이 있을까요?
답변 4
console.log(document.querySelector('meta[property="og:title"]')?.getAttribute("content"));
또는
var og_content = document.querySelector('meta[property="og:title"]')?.getAttribute("content");
alert(og_content);
function get_place_title($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
$html = curl_exec($ch);
curl_close($ch);
// og:title 추출
if (preg_match('/<meta[^>]*property=["\']og:title["\'][^>]*content=["\'](.*?)["\']/i', $html, $m)) {
return trim(str_replace(' : 네이버', '', $m[1]));
}
return false;
}
// 사용
$title = get_place_title('https://m.place.naver.com/hospital/37878766/home');
echo $title; // 우리정형외과의원
먼저 해당 url을 curl로 긁었을때 결과 코드가 뭔지 확인해 보세요.
아마 429이지 않을까 싶네요.
Php만으로는 한계가 많이 있습니다
늦은 답변이지만
플레이스 페이지는 파이썬 사용하지않고서는 페이지 내용을 크롤링하는 것 자체가 안되더군요
답변을 작성하시기 전에 로그인 해주세요.