Data scraping using guzzle HTTP

867 0 0 0

Last Updated : 2024-06-25 18:42:09

This snippet will teach you how to scrape data from any website using guzzle http with php

In this snippet I will teach you how to scraoe data from any website using guzzle http with php 

First 

you have to install guzzle http in your code using the snippet below like this


composer require guzzlehttp/guzzle

Second get the element XPATH

you can get the element XPATH from the console by inspect the element you want -> right click on it -> copy -> copy XPath 

Third

you can use the script below to extract data using the xpath like this


require 'vendor/autoload.php';
$httpClient = new \GuzzleHttp\Client(); // start guzzle http client
$response = $httpClient->get('https://www.usingenglish.com/quizzes/verbs-and-tenses/'); // get the URL you want
$htmlString = (string) $response->getBody(); // get body of that URL page

libxml_use_internal_errors(true); // provide this line to suppress any warnings that don't matter
$doc = new DOMDocument(); // start the DOM document (to create document object model from the page you wanted)
$doc->loadHTML($htmlString); // load the body of the URL in the DOM document
$xpath = new DOMXPath($doc); // create DOM XPATH to define the XPATH you will provide

$urls = $xpath->evaluate('//*[@id="main_content_body"]/div[1]/div[2]/ul/li/a/@href'); // evaluate the XPATH and it it contains any element it will be returned here

foreach ($urls as $element) {
echo $element."<br>";
}




 

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.