SHARE
TWEET

Untitled

a guest Mar 26th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. require 'vendor/autoload.php';
  4.  
  5. use Symfony\Component\DomCrawler\Crawler;
  6. use Symfony\Component\CssSelector\CssSelectorConverter;
  7.  
  8. $url = 'https://www.gamerankings.com/browse.html?site=n64&sort=2&numrev=3&letter=A';
  9. $data = file_get_contents($url);
  10. $crawler = new Crawler($data);
  11.  
  12. $chunks = $crawler->filter('div#content')->filter('div.body')->filter('tr');
  13. $processed = $chunks->each(function($data){
  14.     return processTR( $data->html() );
  15. });
  16. print_r($processed);
  17.  
  18. function processTR($trhtml){
  19.     $data = new Crawler($trhtml);
  20.     $tds = $data->filter('td')->each(function($d){
  21.         return $d->html();
  22.     });
  23.  
  24.     return composeLiteral(
  25.         imageURL($tds[0]),
  26.         console($tds[1]),
  27.         developerYearTitle($tds[2]),
  28.         aggregateScore($tds[3])
  29.     );
  30. }
  31.  
  32. function composeLiteral(...$args){
  33.     $return = [];
  34.     foreach($args as $k => $v){
  35.         foreach($v as $k2 => $v2){
  36.             $return[$k2] = $v2;
  37.         }    
  38.     }
  39.     return $return;
  40. }
  41.  
  42. function imageURL($tdhtml){
  43.     $cr = new Crawler($tdhtml);
  44.     return ['imageurl' => $cr->filter('img')->attr('src')];
  45. }
  46.  
  47. function console($tdhtml){
  48.     $cr = new Crawler($tdhtml);
  49.     return ['platform' => $cr->text()];
  50. }
  51.  
  52. function developerYearTitle($tdhtml){  //this gets the url page, the title, the year, the developer
  53.     $cr = new Crawler($tdhtml);
  54.     $urltogame  = $cr->filter('a')->attr('href');
  55.     $devyear    = $cr->text();
  56.     $parts      = explode(PHP_EOL,$devyear);
  57.     $gamename   = $parts[0];
  58.     $parts      = explode(',',$parts[1]);
  59.     $dev        = $parts[0] ?? 'n/a';
  60.     $year       = $parts[1] ?? 'n/a';
  61.  
  62.     return [
  63.         'gamerankingsurl'   => 'https://www.gamerankings.com' . $urltogame,
  64.         'gamename'          => $gamename,
  65.         'dev'               => trim($dev),
  66.         'year'              => $year
  67.     ];
  68. }
  69.  
  70. function aggregateScore($tdhtml){
  71.     $cr = new Crawler($tdhtml);
  72.     $pieces = explode('%',$cr->text());
  73.  
  74.     return [
  75.         'averagescore'  => $pieces[0] == 'n/aNo Reviews' ? 'n/a' : $pieces[0] . '%',
  76.         'reviewcount'   => $pieces[1] ?? 'n/a'
  77.     ];
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top