I am trying to web scrape this website with Laravel: https://datacvr.virk.dk/soegeresultater?sideIndex=0&enhedstype=virksomhed&antalAnsatte=ANTAL_20_49&virksomhedsstatus=aktiv%252Cnormal&size=10
With other websites, e.g. Wikipedia, the following code works smoothly. However, on this website it returns an error HTML page, where the following error gets shown: "We're sorry but client doesn't work properly without JavaScript enabled. Please enable it to continue." I suppose the reason Javescript is not enabled is because I use Laravel Symfony to web scrape, which is shown below.
<?php
namespace App\Http\Controllers;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\BrowserKit\HttpBrowser;
class CompaniesController extends Controller
{
public function index() {
$client = new HttpBrowser(HttpClient::create());
$crawler = $client->request('GET', 'https://datacvr.virk.dk/soegeresultater?sideIndex=0&enhedstype=virksomhed&antalAnsatte=ANTAL_20_49&virksomhedsstatus=aktiv%252Cnormal&size=10');
return $crawler->html();
}
}
I also tried following this tutorial: https://webmobtuts.com/backend-development/using-laravel-and-symfony-panther-to-scrape-javascript-websites/ Where I tried using the Symfony Panther Client to mock/create a Chrome Client. You can see my code here:
<?php
namespace App\Http\Controllers;
use Symfony\Component\Panther\Client;
class CompaniesController extends Controller
{
public function index() {
$client = Client::createChromeClient(); // create a chrome client
$crawler = $client->request('GET', 'https://datacvr.virk.dk/soegeresultater?sideIndex=0&enhedstype=virksomhed&antalAnsatte=ANTAL_20_49&virksomhedsstatus=aktiv%252Cnormal&size=10');
$client->waitFor('div');
return $crawler->html();
}
}
However, this returns a similar error: "Enable JavaScript and cookies to continue" within an HTML page.
How can you enable JavaScript on web scraping? Do I need to add something to the header of the request, or use a different library?