Buongiorno a tutti,
ho creato con l'aiuto di un piccolo corso online il mio primo scraper, molto ovviamente artigianale ma funziona, solo che riesco solo a memorizzare in un db di prova il listato generale, ma non il dettaglio dell'annuncio.
Posto di seguito questo semplice script, e chiedo se qualcuno potrebbe aiutarmi a capire come si prende il risultato del dettaglio dell'annuncio che si trova nel listing.
Codice PHP:
<?php
use DiDom\Document;
require "./vendor/autoload.php";
#1. Know your goal#2. examine html code (id="home-slider" class="article" elementName="div")
//per class il punto davanti
//$content = $document->find('.home-slider-container')[0];
//per id asterisco
//$content = $document->find('#home-slider')[0];
$document = new Document("https://miosito.it", true);
$content = $document->find('.content')[0];
$postButtons = $content->find('.post-buttons');
$posts = $content->find('.title');
$summary = $content->find('.summary');
$itemImage = $content->find('.item-image');
//$content->find('.title');
/*foreach ($posts as $post) { echo $post->text(); echo "<br><br>";}
foreach ($postButtons as $url) { $hrefs = $url->find('a::attr(href)');
foreach ($hrefs as $href) { echo $href; echo "<br><br>"; }}
*/
for ($i=0; $i<count($postButtons); $i++) { $href = $postButtons[$i]->find('a::attr(href)')[0]; $img = $itemImage[$i]->find('img::attr(data-src)')[0]; $href = $postButtons[$i]->find('a::attr(href)')[0]; $description = $summary[$i]->text();
$title = $posts[$i]->text();
echo "image" . $img; echo "<br>"; echo "<img src='$img'>"; echo "<br>"; echo "title" . $posts[$i]->text(); echo "<br>"; echo "Description" . $description; echo "<br>"; echo "Url:" . $href; echo "<br><br>";}
$servername = "localhost";$username = "root";$password = "root";$dbname = "crawler";
// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}
$sql = "INSERT INTO webpage_details (title, img, href, description)VALUES ('$title', '$img', '$href', '$description')";
if ($conn->query($sql) === TRUE) { echo "New record created successfully";} else { echo "Error: " . $sql . "<br>" . $conn->error;}
$conn->close();