PHP code example of anshu-krishna / html-scraper

1. Go to this page and download the library: Download anshu-krishna/html-scraper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anshu-krishna / html-scraper example snippets



Krishna\DOMNodeHelper;
use Krishna\HTMLScraper;

const TrimmedText = HTMLScraper::Extract_textContentTrim;

$doc = new HTMLScraper();

if(!$doc->load_HTML_file('https://www.royalroad.com/fiction/10073/the-wandering-inn')) {
	echo 'Unable to load data';
	exit(1);
}

$data = [];

$data['title'] = $doc->querySelector_extract(TrimmedText, 'div.fic-title h1[property="name"]', 0);

$data['url'] = $doc->xpath_extract(function($meta) {
	return $meta->getAttribute('content');
}, '//meta[@property="og:url"]', 0);

$data['description'] = htmlspecialchars($doc->querySelector_extract(function(&$div) {
	return trim(DOMNodeHelper::innerHTML($div));
}, 'div.description div[property="description"]', 0));

$data['tags'] = $doc->querySelector_extract(TrimmedText, 'span.tags span[property="genre"]');

var_dump($data);