Guest User

Untitled

a guest
Nov 22nd, 2014
1,042
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function search_rank($keyword, $target, $max_request = 1) {
  4.     $dom = new DOMDocument;
  5.     $url = 'http://search.yahoo.co.jp/search?n=100&p=' . urlencode($keyword);
  6.     for ($i = 0; $i < $max_request; ++$i) {
  7.         @$dom->loadHTMLFile($url);
  8.         $xpath = new DOMXPath($dom);
  9.         foreach ($xpath->query('//*[@id="web"]/ol/li/a/@href') as $j => $node) {
  10.             $value = preg_replace('@^https?+://@', '', $node->nodeValue, 1);
  11.             if (strpos($value, $target) === 0) {
  12.                 return $i * 100 + $j;
  13.             }
  14.         }
  15.         if ('' === $url = $xpath->evaluate('string(//*[@id="pg"]/a[string()="次へ »"]/@href)')) {
  16.             break;
  17.         }
  18.     }
  19.     return false;
  20. }
  21.  
  22. $keyword = 'mpyw';
  23. $target  = 'twitter.com/mpyw';
  24. var_dump(search_rank($keyword, $target, 2));
RAW Paste Data