Skip to content

Instantly share code, notes, and snippets.

@mapyo
Last active December 11, 2015 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapyo/4648606 to your computer and use it in GitHub Desktop.
Save mapyo/4648606 to your computer and use it in GitHub Desktop.
まともサイトの画像&リンクをとって来て、htmlファイルにまとめてしまうやつ。
<?php
//URLは画像とリンクを取得したいURLを入れてください。
$url="http://matome.naver.jp/odai/2127242472660703301";
$res=@file_get_contents($url);
while($res){
$data=getData($res);
for($i=0; $i<count($data["span"]); $i++){
echo "<a href='" . $data["span"][$i]->a["href"] . "'>\n";
echo "<img src='".$data["image"][$i]["src"]."'>\n";
echo "</a>\n";
}
/*
foreach($data["image"] as $image){
echo "<img src='".$image["src"]."'>\n";
}
*/
if($data["nextPage"]){
$res=@file_get_contents($url."?page=".$data["nextPage"]);
sleep(1); //サーバへの負荷対策
}else{
break;
}
}
function getData($html){
$dom=@DOMDocument::loadHTML($html);
$xml=simplexml_import_dom($dom);
$result["image"]=$xml->xpath("//img[@class='MTMItemThumb']");
//var_dump($result["image"][0]["src"]);
$result["span"]=$xml->xpath("//span[@class='mdMTMWidget01ItemCite01Value']");
//var_dump($result["span"][0]->a["href"]);
$pager=$xml->xpath("//div[@class='MdPagination03']");
$current_page=$pager[0]->strong; //太字ページ番号の値
$last_anchor=$pager[0]->a[count($pager[0]->a)-1]; //実際の
if($last_anchor + 1 != $current_page){ //太字のページ番号が最後のページをさしていなければ。。。
$result["nextPage"]=$current_page + 1;
}else{
$result["nextPage"]=null;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment