This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| curl_setopt($curl, CURLOPT_HTTPHEADER, | |
| array( | |
| "<HTTP_HEADER_1>: <HEADER_VALUE>", | |
| "<HTTP_HEADER_2>: <HEADER_VALUE>", | |
| // ... | |
| ) | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| curl_setopt($curl, CURLOPT_USERAGENT, "<USER_AGENT_STRING>"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "url": "https://scrapeme.live/shop/Bulbasaur/", | |
| "image": "https://scrapeme.live/wp-content/uploads/2018/08/001-350x350.png", | |
| "name": "Bulbasaur", | |
| "price": "£63.00" | |
| }, | |
| ... | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| // iterate over all "/shop/page/X" pages and retrieve all product data | |
| for ($paginationNumber = 1; $paginationNumber <= $highestPaginationNumber; $paginationNumber++) { | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/page/$paginationNumber/"); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| $pageHtml = curl_exec($curl); | |
| curl_close($curl); | |
| $paginationHtmlDomParser = HtmlDomParser::str_get_html($pageHtml); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| $productDataLit = array(); | |
| // retrieve the list of products on the page | |
| $productElements = $htmlDomParser->find("li.product"); | |
| foreach ($productElements as $productElement) { | |
| // extract the product data | |
| $url = $productElement->findOne("a")->getAttribute("href"); | |
| $image = $productElement->findOne("img")->getAttribute("src"); | |
| $name = $productElement->findOne("h2")->text; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| // remove all non-numeric characters in the last element of | |
| // the $paginationLinks array to retrieve the highest pagination number | |
| $highestPaginationNumber = preg_replace("/\D/", "", end($paginationLinks)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| // retrieve the HTML pagination elements with | |
| // the ".page-numbers a" CSS selector | |
| $paginationElements = $htmlDomParser->find(".page-numbers a"); | |
| $paginationLinks = []; | |
| foreach ($paginationElements as $paginationElement) { | |
| // populate the paginationLinks set with the URL | |
| // extracted from the href attribute of the HTML pagination element | |
| $paginationLink = $paginationElement->getAttribute("href"); | |
| // avoid duplicates in the list of URLs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| require_once __DIR__ . "/../../vendor/autoload.php"; | |
| use voku\helper\HtmlDomParser; | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/"); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
| $html = curl_exec($curl); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| // initialize the cURL request | |
| $curl = curl_init(); | |
| // set the URL to reach with a GET HTTP request | |
| curl_setopt($curl, CURLOPT_URL, "https://scrapeme.live/shop/"); | |
| // get the data returned by the cURL request as a string | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| // make the cURL request follow eventual redirects, | |
| // and reach the final page of interest | |
| curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "Maria", | |
| "surname": "Williams", | |
| "age": 35 | |
| } |