Last active
October 21, 2021 16:43
-
-
Save andreaselia/962dc9060b3b77308ab61bfd8bfa7449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class ScrapeFunko extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'scrape:funko'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Funko POP! Vinyl Scraper'; | |
/** | |
* The list of funko collection slugs. | |
* | |
* @var array | |
*/ | |
protected $collections = [ | |
'animation', | |
'disney', | |
'games', | |
'heroes', | |
'marvel', | |
'monster-high', | |
'movies', | |
'pets', | |
'rocks', | |
'sports', | |
'star-wars', | |
'television', | |
'the-vault', | |
'the-vote', | |
'ufc', | |
]; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
foreach ($collections as $collection) { | |
$this->scrape($collection); | |
} | |
} | |
/** | |
* For scraping data for the specified collection. | |
* | |
* @param string $collection | |
* @return boolean | |
*/ | |
public static function scrape($collection) | |
{ | |
$crawler = Goutte::request('GET', env('FUNKO_POP_URL').'/'.$collection); | |
$pages = ($crawler->filter('footer .pagination li')->count() > 0) | |
? $crawler->filter('footer .pagination li:nth-last-child(2)')->text() | |
: 0 | |
; | |
for ($i = 0; $i < $pages + 1; $i++) { | |
if ($i != 0) { | |
$crawler = Goutte::request('GET', env('FUNKO_POP_URL').'/'.$collection.'?page='.$i); | |
} | |
$crawler->filter('.product-item')->each(function ($node) { | |
$sku = explode('#', $node->filter('.product-sku')->text())[1]; | |
$title = trim($node->filter('.title a')->text()); | |
print_r($sku.', '.$title); | |
}); | |
} | |
return true; | |
} | |
} |
i want result with html tag ??? how to??
i want result with html tag ??? how to??
->html()
use \Goutte::request instead of Goutte::request if you get not found exception
Couldn't connect to server for "http://localhost/animation".
at vendor/symfony/http-client/Chunk/ErrorChunk.php:65
Couldn't connect to server for "http://localhost/animation".
at vendor/symfony/http-client/Chunk/ErrorChunk.php:65
I have no idea if this still even still works, but make sure you have the FUNKO_POP_URL
in your env file set at the funko website URL, e.g. https://www.funko.com
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ErrorException : Undefined variable: collections
at /var/www/test.local.com/app/Console/Commands/ScrapeFunko.php:57
Using above code:??