Ahmad Naser Turnkey Ecosystem Ahmad Naser
April 9, 2019

scraper for php dom

Published in the Ahmad Naser ecosystem. Estimated reading time: 2 minutes.

download the scrapper from here

https://github.com/ahmadnaser/simple_html_dom

include it in your php script

include_once('./simple_html_dom.php');

$contents=file_get_contents("https://example.com");

$html = str_get_html($contents);
$html_items =  $html->find('#myexampleallsamplescontainer', 0)->innertext; // result: "ok"

 // get article block
    foreach($html->find('div[class^=sorting-item]') as $article) {
       
	   		//extract url
		preg_match( '@class="([^"]+)"@' , $article->outertext, $match2 );
		$_classes = array_pop($match2);
		$item['classes'] = $_classes;
       
		//extract url
		preg_match( '@href="([^"]+)"@' , $article->find('a.shortcode-1', 0)->outertext, $match1 );
		$_url = array_pop($match1);
$pattern = '/^(http|https):\/\/(example\.com\/select\/\?)site(\=)\/?/';
$replacement = '';
$subject = $_url;
$_url = preg_replace($pattern, $replacement, $subject, -1 );
		$item['link'] = $_url;
		
        // get body
        $item['title'] = trim($article->find('span.title', 0)->innertext);
		
		//extract image url
		preg_match( '@data-src="([^"]+)"@' , $article->find('img.b-lazy', 0)->outertext, $match );
		$src = array_pop($match);
		$item['img'] = 'https://example.ps/'.$src;

        $ret[] = $item;
    }

best of luck

Ahmad Naser

get your hosting package now – cp.greenbackend.com?cart.php

 

Weather scraper

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//$city=$_GET['city']; 
//$city=str_replace(" ","",$city);
include_once('./simple_html_dom-master/simple_html_dom.php');
$city="Al-Kamishli";
$contents=file_get_contents("https://www.weather-forecast.com/locations/".$city."/forecasts/latest");
$html = str_get_html($contents);
$html_items =  $html->find('.b-forecast__table-description-cell--js', 0)->innertext; // result: "ok"
$p1 = $html->find('.phrase', 0)->innertext;
//preg_match('/3 Day weather Forecast Summary:<\/b><span class="phrase">(.*?)</s', $contents , $matches);

//echo $matches[1];
echo $html_items;
echo $p1;
 ?>

 

Leave a Reply

Your email address will not be published. Required fields are marked *