Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # scraping books to scrape: https://books.toscrape.com/
- require 'vendor/autoload.php';
- $url = "https://books.toscrape.com/";
- $httpClient = new \GuzzleHttp\Client();
- $response = $httpClient->get($url);
- $htmlString = (string) $response->getBody();
- //add this line to suppress any warnings
- libxml_use_internal_errors(true);
- $doc = new DOMDocument();
- $doc->loadHTML($htmlString);
- $xpath = new DOMXPath($doc);
- $titles = $xpath->evaluate('//ol[@class="row"]//li//article//h3/a');
- $covers = $xpath->evaluate("//img[@class='thumbnail']");
- $extractedTitles = [];
- foreach ($titles as $title) {
- $extractedTitles[] = $title->textContent.PHP_EOL;
- }
- $extractedCovers = [];
- foreach ($covers as $cover){
- $extractedCovers[] = $cover->getAttribute('src');
- }
- $books = [
- 'title' => $extractedTitles,
- 'cover' => $extractedCovers,
- ]
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
- <title>books scrape</title>
- </head>
- <body>
- <div class="container">
- <center>
- <h1 style="text-decoration: underline;">Scraped Books</h1>
- </center>
- <div class="row">
- <?php for ($i=0; $i < count($books['title']); $i++){ ?>
- <div class="col-xl-2 col-lg-2 col-12 mb-5">
- <div class="card">
- <img src="<?=$url . $books['cover'][$i]?>" height="200px" alt="">
- <div class="card-body" style="max-height: 100px; height: 100px; overflow: auto;">
- <span ><?= $books['title'][$i] ?></span>
- </div>
- </div>
- </div>
- <?php } ?>
- </div>
- </div>
- <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
- <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.min.js" integrity="sha384-VHvPCCyXqtD5DqJeNxl2dtTyhF78xXNXdkwX1CZeRusQfRKp+tA7hAShOK/B/fQ2" crossorigin="anonymous"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement