956 0 0 0
Last Updated : 2025-04-28 21:40:05
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>";
}