Skip to content

Instantly share code, notes, and snippets.

@jacobzlogar
Created January 18, 2021 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobzlogar/da1ebf99f267156ff510c4ef6eb8cf7d to your computer and use it in GitHub Desktop.
Save jacobzlogar/da1ebf99f267156ff510c4ef6eb8cf7d to your computer and use it in GitHub Desktop.
webcad scraper
public function handle(Crawler $crawler)
{
$res = Http::withOptions([
'verify' => false,
'debug' => true,
])->get(config('webcad.url'));
$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHtml($res->body());
$tables = $doc->getElementsByTagName('table');
foreach ($tables as $table) {
$rows = $table->getElementsByTagName('tr');
foreach ($rows as $key => $row) {
if (! $key) {
$columns = $crawler->buildColumns($row);
} else {
$this->results[] = $crawler->buildCells($row, $columns);
}
}
}
foreach ($this->results as $result) {
$dispatched_at = Carbon::createFromFormat('m-d-Y H:i:s', $result['Dispatch Time']);
Incident::firstOrCreate([
'incident_number' => $result['Incident No.']['number'],
], [
'type' => $result['Incident Type'],
'location' => $result['Incident Location'],
'municipality' => $result['Municipality'],
'dispatched_at' => $dispatched_at,
'station' => $result['Station'] ?? $result['Agency'],
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment