Skip to content

Instantly share code, notes, and snippets.

@b0n
Created October 5, 2012 22:35
Show Gist options
  • Save b0n/3842865 to your computer and use it in GitHub Desktop.
Save b0n/3842865 to your computer and use it in GitHub Desktop.
簡単!たった10行のコードでHTML取得&解析をするPHPスクリプト
<?php
$url = 'http://www.yahoo.co.jp';
$user_agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
$opts = array(
'http' => array(
'method' => 'GET',
'user_agent' => $user_agent,
));
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
$xml = simplexml_import_dom(DOMDocument::loadHTML($result));
$items = $xml->xpath("//*[@id='topicsfb']//li");
//print_r($items);
foreach($items as $item) {
echo $item->a, PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment