So i have this url $url = "localhost:8000/vehicles" that i want ot fetch through a cron job but the page returns html so i wanna use symfony dom crawler to get all the vehicles instead of regex
At the top of my file i added
use Symfony\Component\DomCrawler\Crawler;
To create a new instance i tried:
$crawler = new Crawler($data);
and i tried
$crawler = Crawler::create($data);
but that gives me an error, also tried adding
Symfony\Component\DomCrawler\Crawler::class,
to the service provider but when i execute the command:
composer dump-autoload
it gives me the following error
In Crawler.php line 66:
Symfony\Component\DomCrawler\Crawler::__construct(): Argument #1 ($node) must be of type DOMNodeList|DOMNode|array|string|null, Illuminate\Foundation\Application given, called in C:\xampp\htdocs\DrostMachinehandel\DrostMachinehandel\vendor\laravel\fr
amework\src\Illuminate\Foundation\ProviderRepository.php on line 208
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
I have no idea how to fix this.
The fucntion for fetching the url is below:
public function handle()
{
$url = SettingsController::fetchSetting("fetch:vehicles");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
$vehicles = $this->scrapeVehicles($data, $url);
Log::debug($vehicles);
curl_close($ch);
}
private function scrapeVehicles(string $data, string $url): array
{
$crawler = Crawler::create($data);
$vehicles = $crawler->filter(".vehicleTile");
return $vehicles;
}
Contents of $data:
CodePudding user response:
Since not been tested, I'm not sure.
Make sure you installed correct package
composer require symfony/dom-crawler
To initiate, use the full path. (since it's not Laravel way(package))
$crawler = \Symfony\Component\DomCrawler\Crawler::create($data);
CodePudding user response:
It looks like you are trying to create a new instance of Crawler by passing it a string as the argument. However, the Crawler class expects the first argument to be a DOMNodeList, DOMNode, array, or string.
If you want to create a new Crawler instance using a string containing HTML, you can do the following:
use Symfony\Component\DomCrawler\Crawler;
$html = '<html><body>...</body></html>';
$crawler = new Crawler($html);
Alternatively, you can use the create static method of Crawler to create a new instance:
$crawler = Crawler::create($html);
In either case, the Crawler instance will allow you to use DOM manipulation methods to traverse and extract data from the HTML.
For example, you can use the filter method to select elements from the HTML, and then use other methods such as text or html to extract their contents:
$title = $crawler->filter('h1')->text();
$description = $crawler->filter('p.description')->html();
I hope this helps!