Advertisement
gigawatt

TCG Player Price Scraper

Oct 7th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. <?php
  2.  
  3. $set = urlencode($argv[1]);
  4. $url = "http://magic.tcgplayer.com/db/search_result.asp?Set_Name=" . $set;
  5. $page = file_get_contents($url);
  6.  
  7. preg_match('|<table width=540 cellpadding=1 cellspacing=0 border=0 align=center>(.*)</table>|Usi', $page, $matches);
  8.  
  9. // Minor formatting tweaks: stripping tags, removing unneeded whitespace
  10. $page = strip_tags($matches[1], '<tr><td>');
  11. $page = str_replace(array("&nbsp;", "\t", "\n", "\r"), "", $page);
  12. $page = preg_replace("|<t[^>]+>|", "", $page);
  13.  
  14. // Actual data formatting
  15. $page = str_replace("</td>", "|",  $page); // Column delimiter
  16. $page = str_replace("</tr>", "\n", $page); // Record delimiter
  17. $page = str_replace("|\n",   "\n", $page); // Remove extra delimiter
  18.  
  19. // Final format: CSV
  20. // Card Name, Casting Cost, Set Name, Rarity, High, Mid, Low
  21.  
  22. $lines  = explode("\n", $page);
  23. $stdout = fopen("php://stdout", "w");
  24. foreach ($lines as $l) {
  25.     fputcsv(STDOUT, explode("|", $l));
  26. }
  27.  
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement