스크래핑,파싱
네이버 xml을 php xml 파싱하는 방법
xemaker
2017. 11. 9. 08:56
<rss version="2.0">
<channel>
<title>Naver Open API</title>
<link>http://search.naver.com</link>
<description>Naver Search Result</description>
<lastBuildDate>Wed, 16 Apr</lastBuildDate>
<total>5455</total>
<start>1</start>
<display>2</display>
<item>
<title>asp가 무엇인가요</title>
<link>
http://openapi.naver.com/?asp
</link>
<description>
asp 는 active server page 입니다.
</description>
</item>
<item>
<title>jsp가 무엇인가요</title>
<link>
http://openapi.naver.com/?jsp
</link>
<description>
jsp 는 java server page 입니다.
</description>
</item>
</channel>
</rss>
<?php
$url="http://localhost/php_xml/naver.xml";
$response = file_get_contents($url);
$object = simplexml_load_string($response);
print_r($object);
$channel = $object->channel;
echo "total=".$channel->total;
echo "\n";
foreach ($channel->item as $value){
$title=$value->title;
echo "title=".$title;
echo "\n";
$link=$value->link;
echo "link=".$link;
echo "\n";
}
?>
<channel>
<title>Naver Open API</title>
<link>http://search.naver.com</link>
<description>Naver Search Result</description>
<lastBuildDate>Wed, 16 Apr</lastBuildDate>
<total>5455</total>
<start>1</start>
<display>2</display>
<item>
<title>asp가 무엇인가요</title>
<link>
http://openapi.naver.com/?asp
</link>
<description>
asp 는 active server page 입니다.
</description>
</item>
<item>
<title>jsp가 무엇인가요</title>
<link>
http://openapi.naver.com/?jsp
</link>
<description>
jsp 는 java server page 입니다.
</description>
</item>
</channel>
</rss>
<?php
$url="http://localhost/php_xml/naver.xml";
$response = file_get_contents($url);
$object = simplexml_load_string($response);
print_r($object);
$channel = $object->channel;
echo "total=".$channel->total;
echo "\n";
foreach ($channel->item as $value){
$title=$value->title;
echo "title=".$title;
echo "\n";
$link=$value->link;
echo "link=".$link;
echo "\n";
}
?>