Skip to content

Instantly share code, notes, and snippets.

Created June 30, 2018 01:58
Show Gist options
  • Save apat/11b35c214f2a1b15aca0208b6b44f2c6 to your computer and use it in GitHub Desktop.
Save apat/11b35c214f2a1b15aca0208b6b44f2c6 to your computer and use it in GitHub Desktop.
How to scrap a hotel website related to own website. It is a example code.
// Mexlike Documentation (Spanish):
// Call dependency. Download page:
require 'simple_html_dom.php';
// Create DOM from URL or file
$html = file_get_html('');
// List of hotels
$wrap_hotels = $html->find('div.prw_meta_hsx_responsive_listing');
* @param Array $attr_image Attributes in array
* @return Image URL
function get_standar_image_url($attr_image) {
// Return default
$url_image = 'nothing';
// Only for style attibutes
if (isset($attr_image['style'])) {
// Remove background-image:url( & ) from background-image:url(URL_DE_IMAGEN)
// You can also use regex
$url_image = substr($attr_image['style'], 21, -2);
} else {
// Nothing to do
$url_image = $attr_image['data-lazyurl'];
// Return result
return $url_image;
// List for Hotels
$list_hotels = array();
// Find all images
foreach($wrap_hotels as $element) {
$hotel = new stdClass();
$hotel->name = $element->find('.property_title', 0)->plaintext;
$hotel->price = $element->find('.price', 0)->plaintext;
$hotel->image_url = get_standar_image_url($element->find('.inner', 0)->attr);
$hotel->href = $element->find('.photo-wrapper a', 0)->href;
array_push($list_hotels, $hotel);
<!DOCTYPE html>
<html lang="en" dir="ltr">
<meta charset="utf-8">
<title>Registro de Hoteles de Isla Mujeres</title>
<link rel="stylesheet" href="">
<style media="screen">
.container {
min-height: 15rem;
position: relative;
.img-fluid {
width: 100%;
.thumbnail {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
.thumbnail img {
position: absolute;
left: 50%;
top: 50%;
height: 100%;
width: auto;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
.thumbnail img.portrait {
width: 100%;
height: auto;
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">Logo</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
<li class="nav-item">
<a class="nav-link" href="#">Hoteles</a>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
<div class="jumbotron thumbnail">
<img class="portrait" src="" alt="">
<div class="container">
<h1 class="display-3 text-light">Hoteles de Isla Mujeres!</h1>
<h3 class="bg-dark text-light">Te presentamos los hoteles más populares de Isla Mujeres. Encontrarás las mejores recomendaciones de nuestra comunidad.</h3>
<div class="container-fluid">
<div class="d-flex flex-wrap">
<?php foreach ($list_hotels as $hotel): ?>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 ">
<div class="wrap-box">
<div class="box-img">
<a href="<?php echo '' . $hotel->href ?>">
<img src="<?php echo $hotel->image_url ?>" class="img-fluid" alt="<?php echo $hotel->name ?>">
<div class="rooms-content">
<h4><a href="<?php echo '' . $hotel->href ?>"><?php echo $hotel->name ?></a></h4>
<p class="price"><?php echo $hotel->price ?> / Por Noche</p>
<?php endforeach; ?>
<script src=""></script>
<script src=""></script>
<script src=""></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment