Created
January 18, 2021 18:17
-
-
Save jacobzlogar/da1ebf99f267156ff510c4ef6eb8cf7d to your computer and use it in GitHub Desktop.
webcad scraper
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
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